Hi Fellow Surfers,
Today I’ll talk a bit about Mongoose which is a NodeJS module to allow use manage your MongoDB models / schemas.
I’ve been working with NodeJS for the last few months when I started my journey on creating my first iPhone app: uSpot.
NodeJS and MongoDB are both very new to me, so of course, I’ve been hitting a lot of blockers due to my beginner experience!
One of them is the use of $addToSet operator that MongoDB provides when updating a document.
This operator allows you to add unique value to an array field on your document.
Let’s say you had a shopping list of a list of items: [ “bananas”, “apples” ] and you were to update the shopping list by adding an item called “bananas”, by using $addToSet, Mongo would check if the value exists already or not, if not, add it to the array.
For example, let’s say you have an array like this:
If you were to do:
The resulting array is:
Why … wait … what?! Now, that’s unexpected!
Guess why that happens?
Now how to go around it?
You can tell Mongoose to not create a new ObjectId, by making sure your mongoose schema is as followed:
Setting false to the _id property gives you the expected result!
Time to get back to code!