question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mongooseInstance.array.addToSet weird behavior

See original GitHub issue

Hello,

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:closed
  • Created 7 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
vkarpov15commented, May 21, 2016

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

1reaction
vkarpov15commented, May 21, 2016

Call addToSet multiple times. In your example in an _.each

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found