is it possible tu $push ObjectId to array if value not exist and $pull if it exist ?

925 views
Skip to first unread message

Maks Yaremenko

unread,
Jul 17, 2017, 5:39:43 PM7/17/17
to Mongoose Node.JS ODM
Hello everyone, i am just trying to update my model with the following way - $push ObjectId to array if value not exist and $pull it if not exist ?
Is it pissible? I know that i can use $addToSet if value not exist in array... but how can i remove the value if it exist, in the same query ?
thx. a lot!

Richard Bateman

unread,
Jul 18, 2017, 1:38:04 AM7/18/17
to mongoo...@googlegroups.com
You might want to ask this in a more generic mongodb group, since it's
not a mongoose-specific question.

That said, I can't think of a way that you could do that; what you
could do is use an unorderedbulkoperation and add two steps: one that
searches for records where the value exists and does a $pull and one
that searches for records which don't contain it and does a $push,
then execute it together.

You could do the same thing but slightly less efficiently just with
two update commands.

Richard
> --
> Documentation - http://mongoosejs.com/
> Plugins - http://plugins.mongoosejs.com/
> Bug Reports - http://github.com/learnboost/mongoose
> Production Examples - http://mongoosejs.tumblr.com/
> StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
> Google Groups -
> https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
> Twitter - https://twitter.com/mongoosejs
> IRC - #mongoosejs
> ---
> You received this message because you are subscribed to the Google Groups
> "Mongoose Node.JS ODM" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mongoose-orm...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages