mongodbvalidationdatabase-designwrite-error

Document validation in MongoDB issue


So I'm very new to MongoDB and of course the feedback on when documents fail validation is somewhat... uninformative. So I'm hopeful anyone might see the error(s) I've made.

The schema is relatively simple and works:

db.createCollection("observations", {
    autoIndexId : true, 
    validator : { 
        $jsonSchema: {
            bsonType: "object", 
            required: [ "observationTimestamp", "family", "species", "numAnimals" ], 
            properties: { 
                observationTimestamp: { 
                    bsonType: "long", 
                    description: "Time that the animal(s) were observed. Use 'Date.parse()' syntax when inserting records. Required field. Example: 'Date.parse('Tue, 12 Dec 1995 16:17:18 GMT')'." 
                }, 
                family: { 
                    bsonType: "string", 
                    description: "Vernacular name of group which species belongs to. Required field. Must be a string." 
                }, 
                species: { 
                    bsonType: "string", 
                    description: "Scientific name of observed animal. Required field. Must be a string." 
                }, 
                numAnimals: { 
                    bsonType: "int", 
                    minimum: 1,
                    description: "Number of animals observed. Required field. Must be an integer." 
                } 
            } 
        } 
    } 
} );

I use "long" here for the 'Date.parse()' as I learned it returns a 64-bit integer. I've also tried using "date" and "timestamp" date types to no avail.

My insert is then:

db.observations.insert([
    {
        "observationTimestamp" : Date.parse("Mon, 25 Dec 1995 12:34:56 GMT"),
        "family" : "Sharks",
        "species" : "Carcharodon carcharias",
        "numAnimals" : 3
    },
    {
        "observationTimestamp" : Date.parse("Tue, 12 Dec 1995 16:17:18 GMT"),
        "family" : "Sharks",
        "species" : "Carcharias taurus",
        "numAnimals" : 4
    }
]);

The error given when the command is run is the generic:

BulkWriteResult({
        "writeErrors" : [
                {
                        "index" : 0,
                        "code" : 121,
                        "errmsg" : "Document failed validation",
                        "op" : {
                                "_id" : ObjectId("5a5e4291aa4da4bdfe1a234c"),
                                "observationTimestamp" : 819894896000,
                                "family" : "Sharks",
                                "species" : "Carcharodon carcharias",
                                "numAnimals" : 3
                        }
                }
        ],
        "writeConcernErrors" : [ ],
        "nInserted" : 0,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})

This is using MongoDB server version 3.6. I'm running on a Windows 10 platform (if that means anything).

The only other thing I could think of was using bsonType 'number' for numAnimals, but that didn't work either.

Any helpful thoughts or comments would be appreciated!


Solution

  • Your validation requires the "observationTimestamp" and "numAnimals" as int and long type.

    So when inserting associate the correct type.

    Something like

    db.observations.insert([
        {
            "observationTimestamp" : NumberLong(Date.parse("Mon, 25 Dec 1995 12:34:56 GMT")),
            "family" : "Sharks",
            "species" : "Carcharodon carcharias",
            "numAnimals" : NumberInt(3)
        }
    ]);