How to add an ISODate type value atomically in MongoDB

  mongodb, question

Direct use of $inc does not seem possible:

{$inc: {"time": 1}}

Error:

Cannot apply $inc modifier to non-number

I don’t know if I have misunderstood the prince’s needs, do you want to add one?ISODateField of type value?
If so,$incIt’s not for this.$incCan only be used to increase or decrease values of numeric types, for example:
Original data:

{ "_id" : ObjectId("537eaa530989f15b7f41cedf"), "i" : 1 }

The operation is as follows:

db.test.update({ i : 1 },{ $inc : { i : 2 }})

The result is:

{ "_id" : ObjectId("537eaa530989f15b7f41cedf"), "i" : 3 }

If you want to add a new field, you can use the$pushAgain, the above example illustrates:

db.test.update({ i : 3 },{ $push : {'time' : new ISODate("2014-05-23")}})

The result is:

{ "_id" : ObjectId("537eaa530989f15b7f41cedf"), "i" : 3, "time" : [  ISODate("2014-05-23T00:00:00Z") ] }