I'm currently learning about mongodb, I have gone through the documentation and attempted to insert a document page with referenced id's of other documents, but unfortunately, my validation fails. Here are the schema validation rules I have defined:
db.createCollection("page", {
capped: true,
size: 4500000000,
max: 6,
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "title", "url", "elements" ],
additionalProperties: false,
properties: {
title: {
bsonType: "object",
required: [ "content_id" ],
properties: {
content_id: {
bsonType: "objectId"
}
}
},
url: {
bsonType: "string"
},
elements: {
bsonType: "array",
items: {
bsonType: "object",
required: [ "order" , "element_id" ],
properties: {
order: {
bsonType: "int"
},
element_id: {
bsonType: "objectId"
}
}
}
}
}
}
}
});
I am attempting to insert the following data (where content and document already contain the necessary id's):
var page1 = ObjectId();
db.page.insertOne(
{
"_id": page1,
"title": {
"content_id": content5
},
"url": "/home",
"elements": [
{
"order": 1,
"element_id": element1
},
{
"order": 2,
"element_id": element2
},
{
"order": 3,
"element_id": element3
},
{
"order": 4,
"element_id": element4
}
]
}
);
Can anyone advise on why this is generating an error? I seem to be having trouble understanding where the issue lies. Is there something wrong with the schema based on what I'm trying to insert?
2020-04-07T18:55:35.513+0200 E QUERY [js] WriteError({
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"_id" : ObjectId("5e8c72698d808f037e6adede"),
"title" : {
"content_id" : ObjectId("5e8c72128d808f037e6aded6")
},
"url" : "/home",
"elements" : [
{
"order" : 1,
"element_id" : ObjectId("5e8c724d8d808f037e6adeda")
},
{
"order" : 2,
"element_id" : ObjectId("5e8c724d8d808f037e6adedb")
},
{
"order" : 3,
"element_id" : ObjectId("5e8c724d8d808f037e6adedc")
},
{
"order" : 4,
"element_id" : ObjectId("5e8c724d8d808f037e6adedd")
}
]
}
}) :
WriteError({
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"_id" : ObjectId("5e8c72698d808f037e6adede"),
"title" : {
"content_id" : ObjectId("5e8c72128d808f037e6aded6")
},
"url" : "/home",
"elements" : [
{
"order" : 1,
"element_id" : ObjectId("5e8c724d8d808f037e6adeda")
},
{
"order" : 2,
"element_id" : ObjectId("5e8c724d8d808f037e6adedb")
},
{
"order" : 3,
"element_id" : ObjectId("5e8c724d8d808f037e6adedc")
},
{
"order" : 4,
"element_id" : ObjectId("5e8c724d8d808f037e6adedd")
}
]
}
})
WriteError@src/mongo/shell/bulk_api.js:458:48
mergeBatchResults@src/mongo/shell/bulk_api.js:855:49
executeBatch@src/mongo/shell/bulk_api.js:919:13
Bulk/this.execute@src/mongo/shell/bulk_api.js:1163:21
DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:264:9
@(shell):1:1
Your insights are greatly appreciated.