UM1979
UM1979

Reputation: 162

MongoDB + NodeJS: Document failed validation & Data Types behaviour

I am new to MongoDB and NodeJS,

When i try to create the JsonSchema with data types, string, integer, date and bool, it is created but always throwing an error as document validation error while inserting the data, So i changed the bsonType of one data type to number, then it started creating collection records, but the observation is it is storing as Double datatype, I read somewhere in the stackoverflow, that it stores like that only, but my question is why is this behavior? WHY THE ERROR IS NOT BEING THROWN AT THE TIME OF CREATION OF THE JSONSCHEMA but it is throwing at the time of data insertion?

Also, if we have nested objects let us say, Customer object with Address as nested object, the main object's int/number values are stored as Double where as inside the address object's pincode storing as Int32. This is also very confusing. what is the difference between these objects but the structure of the schema is same.

What are the other ways to implement and having proper validated schema for MongoDB.

> 

db.getCollectionInfos({name:"companysInt1s1"})
[
        {
                "name" : "companysInt1s1",
                "type" : "collection",
                "options" : {
                        "validator" : {
                                "$jsonSchema" : {
                                        "bsonType" : "object",
                                        "required" : [
                                                "tin"
                                        ],
                                        "properties" : {
                                                "tin" : {
                                                        "bsonType" : "int",
                                                        "minLength" : 2,
                                                        "maxLength" : 11,
                                                        "description" : "must be a string and is not required, should be 11 characters length"
                                                }
                                        }
                                }
                        }
                },
                "info" : {
                        "readOnly" : false,
                        "uuid" : UUID("27cba650-7bd3-4930-8d3e-7e6cbbf517db")
                },
                "idIndex" : {
                        "v" : 2,
                        "key" : {
                                "_id" : 1
                        },
                        "name" : "_id_",
                        "ns" : "invoice.companysInt1s1"
                }
        }
]
> db.companysInt1s1.insertOne({tin:22222})
2019-02-14T15:04:28.712+0530 E QUERY    [js] WriteError: Document failed validation :
WriteError({
        "index" : 0,
        "code" : 121,
        "errmsg" : "Document failed validation",
        "op" : {
                "_id" : ObjectId("5c653624e382c2ec16c16893"),
                "tin" : 22222
        }
})
WriteError@src/mongo/shell/bulk_api.js:461:48
Bulk/mergeBatchResults@src/mongo/shell/bulk_api.js:841:49
Bulk/executeBatch@src/mongo/shell/bulk_api.js:906:13
Bulk/this.execute@src/mongo/shell/bulk_api.js:1150:21
DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:252:9
@(shell):1:1  

Am i missing something or any other documentation should i be following? Appreciate your guidance...

Upvotes: 0

Views: 644

Answers (1)

Abdelrahman Hossam
Abdelrahman Hossam

Reputation: 538

You need to insert as NumberInt.

when you run this

db.companysInt1s1.insertOne({tin:22222})

you are actually inserting tin as float.

so the correct way to do it is

db.companysInt1s1.insertOne({tin: NumberInt(22222) })

Upvotes: 1

Related Questions