benjaminbutton
benjaminbutton

Reputation: 153

MongoDB validation fails

I've used the validation rules for MongoDB, but validation fail on every insert.

db.createCollection("mycollection",{
validator:{
    $and:[ 
        {name: {$type:"string"}}, 
        {age: {$type:"int"}}   
    ]
}})

When add an entry

db.mycollection.insertOne({name:"Joseph", age: 18})

But I obtain always an error, with a generic message "Document failed validation". Any idea? Thanks.

Upvotes: 1

Views: 2263

Answers (1)

Dmytro
Dmytro

Reputation: 66

In mongodb the default type for int is Double. So, you can set type Double in validation rule or try

db.mycollection.insertOne({name:"Joseph", age: NumberInt(18)}))

Upvotes: 4

Related Questions