$jsonSchema
New from version 4.0.
Not supported by Elastic cluster.
The $jsonSchema operator in Amazon DocumentDB is used to filter documents based on a specified JSON schema. This operator allows you to query documents that match a particular JSON schema, ensuring that the retrieved documents adhere to specific structural and data type requirements.
Using the $jsonSchema evaluation query operator as part of a collection creation, you can validate the schema of the documents being inserted into the collection. See Using JSON schema validation for additional information.
Parameters
-
required(array): Specifies the required fields in the document. -
properties(object): Defines the data type and other constraints for each field in the document.
Example (MongoDB Shell)
The following example demonstrates the use of the $jsonSchema operator to filter the employees collection to only retrieve documents that have the name, employeeId and age fields, and the employeeId field is of type string.
Create sample documents
db.employees.insertMany([ { "name": { "firstName": "Carol", "lastName": "Smith" }, "employeeId": "1" }, { "name": { "firstName": "Emily", "lastName": "Brown" }, "employeeId": "2", "age": 25 }, { "name": { "firstName": "William", "lastName": "Taylor" }, "employeeId": 3, "age": 24 }, { "name": { "firstName": "Jane", "lastName": "Doe" }, "employeeId": "4" } ]);
Query example
db.employees.aggregate([ { $match: { $jsonSchema: { required: ["name", "employeeId", "age"], properties: { "employeeId": { "bsonType": "string" } } } }} ]);
Output
{ "_id" : ObjectId("6908e8b61f77fc26b2ecd26f"), "name" : { "firstName" : "Emily", "lastName" : "Brown" }, "employeeId" : "2", "age" : 25 }
Code examples
To view a code example for using the $jsonSchema command, choose the tab for the language that you want to use: