直接使用 $inc 似乎不可以:
{$inc: {"time": 1}}
错误:
Cannot apply $inc modifier to non-number
拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...
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:
ISODate
$inc
{ "_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:
$push
db.test.update({ i : 3 },{ $push : {'time' : new ISODate("2014-05-23")}})
{ "_id" : ObjectId("537eaa530989f15b7f41cedf"), "i" : 3, "time" : [ ISODate("2014-05-23T00:00:00Z") ] }
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:
Operation is as follows:
The result is:
If you want to add a new field, you can use
$push
, as explained in the above example:The result is: