2

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!

2
  • Try "observationTimestamp" : NumberLong(Date.parse("Mon, 25 Dec 1995 12:34:56 GMT")) and "numAnimals" : NumberInt(4) Commented Jan 16, 2018 at 18:44
  • @Veeram ... that worked. Umm... If you want to add this as a full answer I'll go ahead and accept it! Commented Jan 16, 2018 at 18:59

1 Answer 1

1

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)
    }
]);
Sign up to request clarification or add additional context in comments.

Comments

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.