I'm trying to add new field (LastLoginDate of type Date) to a existing collection. Here is my sample script:

db.createCollection( "MyTestCollection",
   { "validator": { "$or":
       [
          { "username": { "$type": "string" } },
          { "notes": { "$type": "string" } }
       ]
    }
   }
)

db.getCollectionInfos({name: "MyTestCollection"});
  [
     {
        "name" : "MyTestCollection",
        "options" : {
           "validator" : {
              "$or" : [
                 {
                    "username" : {
                       "$type" : "string"
                    }
                 },
                 {
                    "notes" : {
                       "$type" : "string"
                    }
                 }
              ]
           }
        }
     }
  ]

What is the best way to add new field LastLoginDate : { $type: "date" }, to this existing collection "MyTestCollection".

Adding new document or updating existing collection with new field may create this field. But i'm not sure how to enforce the date type on the new field. After adding new filed, if i execute the following command again, it doesn't show type validator for newly added field.

Solution 1

I "should" probably prefix this with one misconception in your question. The fact is MongoDB differs from traditional RDBMS in that it is "schemaless" and you do not in fact need to "create fields" at all. So this differs from a "table schema" where you cannot do anything until the schema changes. "Validation" however is a different thing as well as a "still" relatively new feature as of writing.

If you want to "add a validation rule" then there are methods which depend on the current state of the collection. In either case, there actually is no "add to" function, but the action instead is to "replace" all the validation rules with new ones to specify. Read on for the rules of how this works.

Existing Documents

Where the collection has existing documents, as noted in the documentation

Existing Documents

You can control how MongoDB handles existing documents using the validationLevel option.

By default, validationLevel is strict and MongoDB applies validation rules to all inserts and updates. Setting validationLevel to moderate applies validation rules to inserts and to updates to existing documents that fulfill the validation criteria. With the moderate level, updates to existing documents that do not fulfill the validation criteria are not checked for validity.

This and the following example section are basically saying that in addition to the options on .createCollection() you may also modify an existing collection with documents, but should be "wary" that the present documents may not meet the required rules. Therefore use "moderate" if you are unsure the rule will be met for all documents in the collection.

In order to apply, you use the .runCommand() method at present to issue the "command" which sets the validation rules. Which is "validationLevel" from the passage above.

Since you have existing rules, we can use the `.getCollectionInfos() to retrieve them and then add the new rule and apply:

let validatior = db.getCollectionInfos({name: "MyTestCollection"})[0].options.validator;

validator.$or.push({ "LastLoginDate": { "$type": "date" } });

db.runCommand({
  "collMod": "MyTestCollection",
  "validator": validator,
  "validationLevel": "moderate"
});

Of course as noted before, that if you are confident the documents all meet the conditions then you can apply "strict" as the default instead.

Empty Collection

If in the case is that the collection is actually "empty" with no documents at all or you may "drop" the collection since the current data is not of consequence, then you can simply vary the above and use .createCollection() in combination with .drop():

let validatior = db.getCollectionInfos({name: "MyTestCollection"})[0].options.validator;

validator.$or.push({ "LastLoginDate": { "$type": "date" } });

db.getCollection("MyTestCollection").drop();

db.createCollection( "MyTestCollection", { "validator": validator });

Solution 2

let previousValidator = db.getCollectionInfos({name: "collectionName"})[0].options.validator;
# push the key to required array
previousValidator.$jsonSchema.required.push("isBloodReportAvailable")

let isBloodReportAvailabl = {"bsonType" : "bool", "description" : "must be an bool object and is optional" }
# add new property to validator
previousValidator1.$jsonSchema.properties['isBloodReportAvailable'] = isBloodReportAvailabl

db.runCommand({
  "collMod": "collectionName",
  "validator": previousValidator,
});