I am looking to:
- Retrieve specific items from collection X;
- Retrieve other items from X;
- Make the items from step #1 have the same value as the items from step #2
- Make the items from step #2 have the same value as the items from step #1
Essentially, I need to perform a swap.
However, my initial attempt resulted in updating all records to On
instead of swapping the values.
The code I used:
var cursorOn = db.car.find({T: 'e46dba7', State: "On"});
var cursorOff = db.car.find({T: 'e46dba7', State: "Off"});
while (cursorOn.hasNext()) {
doc = cursorOn.next();
db.car.update(
{_id: doc._id},
{$set:{"State": "Off"}},
{writeConcern: { wtimeoutMS: 50000 }}
);
}
while (cursorOff.hasNext()) {
doc = cursorOff.next();
db.car.update(
{_id: doc._id},
{$set:{"State": "On"}},
{writeConcern: { wtimeoutMS: 50000 }}
);
}
I also attempted using async/await, but encountered a SyntaxError:
let cursorOn = await db.car.find({T: 'e46dba7', State: "On"});
let cursorOff = await db.car.find({T: 'e46dba7', State: "Off"});
The error message was:
SyntaxError: Unexpected identifier at /root/MongoScripts/update.js
How can I adjust the code to ensure a synchronous operation for the desired result? Thank you.