Actually i want to remove one value from an array and insert new value in another array.
const query = { “order.userId”: userId };
const removeOne = {$pull:{ [removeValue] :id}};
const updateOne = {$push:{[insertValue]:{ $each:[id] , $position: insertPosition}}};
const result = Order.updateMany(query , removeOne , updateOne).catch(err=>{
console.log(err);
});
this is my code.
Hello @kuldeep_saini, you can do something like this:
Suppose you have a document like this:
{
_id: 1,
numbers: [ 4, 12, 55 ],
strings: [ "red", "green", "blue", "white" ]
}
To add to numbers
array and remove from strings
array the update would be (as run in mongo
shell, and the NodeJS driver query syntax is likely to be slightly different):
db.collection.updateOne(
{ },
{
$push: { numbers: 777 },
$pull: { strings: "white" }
}
)
1 Like
Hi @kuldeep_saini,
Need to merge both $pull
and $push
object in one and pass it in the second parameter,
const result = Order.updateMany(
query,
{
...removeOne,
...updateOne
}
).catch(err => {
console.log(err);
});