I want to update a field and array inside a mongodb collection.
myCollection.
{
position:String,
users : [String]
}
I know how to update the position using:
myCollection.updateOne({position:position})
I know hot to update the users array using:
myCollection.updateOne({position:position}, { $addToSet: {users:users})
But how to update the two at the same time?
Thanks a lot guys. Backend noob here!
CodePudding user response:
The first parameter of updateOne is the filter, so by running this query:
myCollection.updateOne({ position }, {
$set: { position: newPosition },
$addToSet: { users }
})
You should update the position of the document with position to newPosition and add a new users to the users array
