Skip to content

Instantly share code, notes, and snippets.

@pablocattaneo
Last active January 7, 2019 11:52
Show Gist options
  • Save pablocattaneo/25ca8a25abfaf619064688c34677a7d6 to your computer and use it in GitHub Desktop.
Save pablocattaneo/25ca8a25abfaf619064688c34677a7d6 to your computer and use it in GitHub Desktop.
How to add collection validation in MongoDb? #mongoDb #mongoDbCollectionValidation #mongoShell Source: 1. https://www.udemy.com/mongodb-the-complete-developers-guide/learn/v4/t/lecture/11758316?start=0 2. https://docs.mongodb.com/manual/core/sche
db.createCollection("collectionName", {
validator: {
$jsonSchema: {
bsonType: "object", // everything in the collection should be a valid Mongo Document
required: ["fileld1", "fiel2""],
properties: {
fileld1: {
bsonType: "bsonType",
description: "description"
}
fileld2: {
bsonType: "object",
description: "description"
required: ["fileld1", "fiel2""],
properties: { ...
}
}
}
}
}
db.createCollection("usersToSendEmail", {
validator: {
$jsonSchema: {
bsonType: "object", // everything in the collection should be a valid Mongo Document
required: ["name", "lastName","email", "birth"],
properties: {
name: {
bsonType: "string",
description: "Must be a string and is required"
},
birth: {
bsonType: "date",
description: "Must be a date type and is required"
}
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment