0

I have a Tags collection which contains documents of the following structure:

{
  word:"movie", //tag word
  count:1        //count of times tag word has been used
}

I am given an array of new tags that need to be added/updated in the Tags collection:

["music","movie","book"]

I can update the counts all Tags currently existing in the tags collection by using the following query:

db.Tags.update({word:{$in:["music","movies","books"]}}, {$inc:{count:1}}), true, true);

While this is an effective strategy to update, I am unable to see which tag values were not found in the collection, and setting the upsert flag to true did not create new documents for the unfound tags.

This is where I am stuck, how should I handle the bulk insert of "new" values into the Tags collection? Is there any other way I could better utilize the update so that it does upsert the new tag values?

(Note: I am using Node.js with mongoose, solutions using mongoose/node-mongo-native would be nice but not necessary)

Thanks ahead

1 Answer 1

1

The concept of using upsert and the $in operator simultaneously is incongruous. This simply will not work as there is no way to different between upsert if *any* in and upsert if *none* in.

In this case, MongoDB is doing the version you don't want it to do. But you can't make it change behaviour.

I would suggest simply issuing three consecutive writes by looping through the array of tags. I know that's it's annoying and it has a bad code smell, but that's just how MongoDB works.

Sign up to request clarification or add additional context in comments.

1 Comment

this loop sucks indeed... I am using python which is non compiled and loops are going to slow down my code significantly as I constantly need to update a long list of documents. I am exploring other document-based database to see if I cannot find something better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.