mongooseInstance.array.addToSet weird behavior
See original GitHub issueHello,
I have the following code:
mongooseInstance.array.addToSet({user: uid, name: "theName"}); mongooseInstance.save(function(err,mongooseInstance){ if(err){ //DO something } else { //do something else with mongooseInstance } });
Knowing that the mongooseInstance.array already contain the object {user: uid, name: “theName”}.
There isnt duplicate entries in the db but my mongooseInstance after the save contain two object {user: uid, name: “theName”}.
mongooseInstance.array = [{user: uid, name: "theName"},{user: uid, name: "theName"}]
I can get what i want by querying the mongooseInstance again but it doesnt seems really effective.
Issue Analytics
- State:
- Created 7 years ago
- Comments:8
Top Results From Across the Web
Mongoose: why is $addtoset adding incorrect ObjectId
I'm getting very strange behaviour however. $addtoset is appending an objectId to the array, but it's not the one that I'm passing to...
Read more >Mongoose v6.8.2: API docs
An array containing all connections associated with this Mongoose instance. By default, there is 1 connection. Calling createConnection() adds a connection to ...
Read more >Mongoose API v4.4.14
Returns an array of model names created on this instance of Mongoose. Returns: <Array>. Note: Does not include names of models created using ......
Read more >MongoDB's $addToSet Operator: Using Mongo Shell and ...
In the following scenarios, we will explore the behaviour of $addToSet with different values passed to it. Scenario 1: Adding one item an...
Read more >Missing _id in one element of array - MongoDB
I've encountered a strange behavior that I can't explain nor reproduce. There are documents in my db that contain an array with either...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Not sure how well documented it is but you can do doc.array.addToSet() followed by doc.save() and that does a $addToSet under the hood
Call addToSet multiple times. In your example in an
_.each