在更新中执行$ set和$ rename

我有以下文档

{“value”:5}

我想要更新导致以下内容:

{“value_old”:5,“value”:3}

我正在尝试使用以下更新查询来实现此目的

{ $rename: {"value": "value_old"} }, { $set :{ "value": (wtv new value was sent to the function) 3 } 

此代码似乎并没有工作….我不想执行两个单独的更新查询

我已经尝试过聚合,并在它的结尾$ out,但导致删除整个集合,我只想更新一个单独的文档。

我这样做的原因是因为我需要操作是primefaces的。 我只是读db.eval(),它看起来很有前途,因为它从读写locking数据库。 有谁知道如何使用db.eval()来执行2更新?

如果你使用MongoClient,你可以以asynchronous的方式处理。 例如:

 const MongoClient = require('mongodb').MongoClient; function updateDB (newVal) { return MongoClient.connect(MONGODB_URL) .then( (database) => { return Promise.all([ database.collection('yourCollection').update({id: 123}, {$rename: {'value': 'value_old'}}), database.collection('yourCollection').update({ $set: {'value': newVal} }) ]) .then(() => { database.close(true); }) .then(() => { return `new value: ${newVal} has been updated in : ${whatever}`; }) .catch((err) => { database.close(true); throw err; }); }) } 

这是我所做的,应该是安全的执行许多操作,并closures数据库,只有当所有完成。