直接使用 $inc 似乎不可以:
{$inc: {"time": 1}}
错误:
Cannot apply $inc modifier to non-number
怪我咯2017-04-24 09:11:55
I don’t know if you have misunderstood the prince’s needs. Do you want to add a new field with a ISODate
type value?
If so, $inc
并不是用来做这个的。$inc
can only be used to increase or decrease numeric type values, for example:
Original data:
{ "_id" : ObjectId("537eaa530989f15b7f41cedf"), "i" : 1 }
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 $push
, as explained in the above example:
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") ] }