how to validate mongodb embedded document

Viewed 3091

MongoDB 3.2 has this nice validation feature. However, in this doc it only shows me how to do the validation on first level fields. If I have the following embedded document to insert, how could I set the validation rule?

{"name": {
          "first_name": "xx",
          "last_name": "yy"
         }
}

I've tried the following but it doesn't work,

db.createCollection( "name_collection",
    { validator: { $and:
        [
            {name.first_name: {$type: "string"}},
            {name.last_name: {$type: "string"}}
        ]
    }
)

Thanks in advance.

2 Answers
Related