search

Home  >  Q&A  >  body text

mongodb 不管存在不存在该值都会返回1?

update({"tag":"9_14712612489","funding_id":9,"inventory_num":{"$gt":0}},{"$inc":{"inventory_num":-1}})

就是查找大于0的然后-1
但是这个数据不存在 也会返回true 是我程序接住了异常还是本来就是这样?

某草草某草草2839 days ago697

reply all(1)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-02 09:23:43

    The main thing here is the different understanding of true and false. true means that the update operation was successful, but it does not necessarily update any records. False means that the update operation failed, which may be due to various system errors and exceptions, such as being unable to connect to mongod.

    > db.user.update({"username": "test"}, {"source": "ios"})
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.user.update({"username": "xxxx"}, {"source": "ios"})
    WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })

    Like the example above, both return true because their operations are successful, regardless of whether there is an update

    reply
    0
  • Cancelreply