什么是 MongoDB 与 RethinkDB 的 r.row 的等价物?
What is MongoDB's equivalent to RethinkDB's r.row?
我正在做的项目使用MongoDB,但我通常使用RethinkDB。我正在尝试编写一个操作,它将获取文档 属性 的当前值并从中减去 1。在 reQL 中,这将是 r.db('db').table('table').get('documentId').update({propToUpdate: r.row('propToUpdate').sub(1)})
。如何在 MongoDB 中执行相同的功能?
我认为你可以使用 $inc 运算符:
db.products.update(
{ sku: "abc123" },
{ $inc: { quantity: -1} }
);
作为示例摘自 here。
其中"db"是数据库,(比如用myDatabase),"products"是你要查询的集合名,"sku"是你要查询的字段,"quantity" 就是你要 increment/decrement 的字段。
我正在做的项目使用MongoDB,但我通常使用RethinkDB。我正在尝试编写一个操作,它将获取文档 属性 的当前值并从中减去 1。在 reQL 中,这将是 r.db('db').table('table').get('documentId').update({propToUpdate: r.row('propToUpdate').sub(1)})
。如何在 MongoDB 中执行相同的功能?
我认为你可以使用 $inc 运算符:
db.products.update(
{ sku: "abc123" },
{ $inc: { quantity: -1} }
);
作为示例摘自 here。
其中"db"是数据库,(比如用myDatabase),"products"是你要查询的集合名,"sku"是你要查询的字段,"quantity" 就是你要 increment/decrement 的字段。