search

Home  >  Q&A  >  body text

在 MongoDB 中如何原子地增加一个 ISODate 类型的值

直接使用 $inc 似乎不可以:

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

错误:

Cannot apply $inc modifier to non-number
高洛峰高洛峰2763 days ago667

reply all(1)I'll reply

  • 怪我咯

    怪我咯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") ] }
    

    reply
    0
  • Cancelreply