I use the following update
command to model:
Room.update({_id: roomId, status: 'live'}, {$push: {"users" : {_id: user._id, loginName: user.loginName}}}, {}, function(err, room) {
model:
{
_id: "56c0d9e332f6ddc80ec7271c",
name: "Reqqqq2",
creator: "MegaDaddgy",
status: "live",
__v: 0,
users: [
{
_id: "56c0a986eeb118741109a45f",
loginName: "MegaDaddgy"
},
{
_id: "56bf96f56a0c220812f055e6",
loginName: "Maad"
}
]
}
I don't want to add duplicate fields to users
, How can I do this?
The $addToSet
could prevent the duplicate entry present in the users
Room.update(
{_id: roomId, status: 'live'},
{$addToSet:
{"users" : {_id: user._id, loginName: user.loginName}}}, function(err, room) {
Another way could be done through $ne
same as Blakes mentioned in the comment as below
Room.update({ "_id": roomId, "status": "live", "users._id": { "$ne": user._id } }, { "$push": { "users": { "_id": user._id, "loginName": user.name } } })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With