0

I created a collection with validation rules using the following syntax:

>db.createCollection("CollName", {
   validator: {
      $jsonSchema:
{
    bsonType: "object",
    properties: {
        a1: {
            bsonType: "string",
        },
        a2:{
            bsonType: "string",
        },
        a3: {
            bsonType: "array",
            items: {
                bsonType: "object",
                  properties: {
                    b1: {
                        bsonType: "string",
                    },
                    b2: {
                        bsonType: "string",
                      }
                }
            }
        }

    }
}
}})

And I want to insert some documents into it. However, I am not able to find the correct syntax. I tried the following:

>  db.CollName.insert({
...  a1:"122234343",
...  a2: "name1",
...  a3: [b1: "aaa1111", b2: "bbb222"]
... })

2020-05-28T12:33:50.052+0200 E  QUERY    [js] uncaught exception: SyntaxError: missing ] after element list :
@(shell):4:8

>  db.CollName.insert({
...  a1:"122234343",
...  a2: "name1",
...  a3: ["aaa1111", "bbb222"]
... })

WriteResult({
    "nInserted" : 0,
    "writeError" : {
        "code" : 121,
        "errmsg" : "Document failed validation"
    }
})

How can I find the correct syntax?

1 Answer 1

1

Try this:

db.CollName.insert({
 "a1":"122234343",
 "a2": "name1",
 "a3": [{"b1": "aaa1111", "b2": "bbb222"}]
})
Sign up to request clarification or add additional context in comments.

1 Comment

An explanation would be in order. E.g., what is the idea/gist? What did you change and why? What was the problem in the original? From the Help Center: "...always explain why the solution you're presenting is appropriate and how it works". Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.