Batch update with Mongoose - node.js

I'm pulling data from a RETS(XML) feed and saving it in a local MongoDB using node and mongoose.
Periodically I need to update the documents and delete the inactive ones as well as add new ones. Rather than making multiple queries to Mongo or the RETS server, I was pulling both and looping through the data.
This works fine but is there a way to save the Mongoose results back to the database with updates and inserts? Or do I need to find each document and update it individually?

On MongoDB, to update multiple documents (not just one) using Mongoose you can use the multi option:
Model.updateMany({
size: 'lage'
}, {
$set: { size: 'large' }
});
See more on in the Mongoose documentation for updating documents and here

For completeness, If any one has multiple query conditions and want to add new fields for every matching documents of query condition then we can go with
var bulk = Person.collection.initializeUnorderedBulkOp();
bulk.find(query1).update(update1);
bulk.find(query2).update(update2);
bulk.execute(callback);
In following documentation, it is said that db.collection.initializeUnorderedBulkOp()
Initializes and returns a new Bulk() operations builder for a
collection. The builder constructs an unordered list of write
operations that MongoDB executes in bulk. MongoDB executes in
parallel the write operations in the list.
https://docs.mongodb.org/v3.0/reference/method/db.collection.initializeUnorderedBulkOp/

Related

I want to update multi document in mongodb

I have a collection in mongoDB that hold 66000 documents on it, Now I want to add new filed to all theses document a uniqueID field which take this form S-1111 and the number increase for the next document
I have tried to make a call to DB and update each one in order but it take to much time because this is a lot of request to mongoDB , so is there is another way to do that fast
I am working with Node and mongoose

Delete all documents in MongoDB collections in Mongo shell

I would like to delete all documents in all MongoDB collections. Is there a way to delete all documents in all collections.
I was using db.collection.remove({}) but it only removes all documents in one collection. Is there any command to do? I'm using NodeJS mostly, maybe there is a chance to use NodeJS to delete all documents in all collections?
Sorry if the question is dumb, just started working in MongoDB.
As already suggested - You can either use .dropDatabase() to drop entire database or .collection.drop() to drop a collection or if it's just to delete all documents in all collections then you need to iterate on list of collections and implement either .collection.remove() or .collection.deleteMany() or .findAndModify() without any filter in query condition.
To delete documents in each collection individually :
first list all collection names using .getCollectionNames() and then remove documents.
let colls = db.getCollectionNames() // Mongo shell can accept .Js Func's, if you've more collections you can use parallel as well
colls.forEach(eachColl => db[eachColl].remove({})) // or .deleteMany() or . findAndModify()
Doing this way, you'll still have the database and empty collections existing on MongoDB server. Maybe you can comeback after sometime check list of collections available or maybe rename few etc.
But if you just simply don't want to look at collection names that use to exist in any near future, go ahead with drop commands preferable drop database as you wanted to delete all docs from all collections - why it's preferred ? is because unlike SQL databases MongoDB automatically creates a database and a collection if you write a document for the first time to a collection in a DB. So in MongoDB you might not need to maintain databases with empty collections.
Assume you're querying on collection named girlfriend which is in mylife database - Let's say it's already deleted/missing/never existed then .find() would return [] empty array same like querying on empty collection on a DB - this is the advantage with MongoDB as it doesn't throw an error on mismatched names.

mongodb findAndUpdate : get original document value

We are trying to implement versioning of documents in mongodb for our project using nodejs. For this, while we perform the findOneAndUpdate command we want to find the difference between the existing document and the new values being updated into it. The returnNewDocument parameter returns the new document after updating is there any way to get the base document ( the one before update is performed). Or is there any better way to perform mongodb document versioning?

Mongoose bulk insert or update documents

I am working on a node.js app, and I've been searching for a way around using the Model.save() function because I will want to save many documents at the same time, so it would be a waste of network and processing doing it one by one.
I found a way to bulk insert. However, my model has two properties that makes them unique, an ID and a HASH (I am getting this info from an API, so I believe I need these two informations to make a document unique), so, I wanted that if I get an already existing object it would be updated instead of inserted into the schema.
Is there any way to do that? I was reading something about making concurrent calls to save the objects, using Q, however I still think this would generate an unwanted load on the Mongo server, wouldn't it? Does Mongo or Mongoose have a method to bulk insert or update like it does with insert?
Thanks in advance
I think you are looking for the Bulk.find(<query>).upsert().update(<update>) function.
You can use it this way:
bulk = db.yourCollection.initializeUnorderedBulkOp();
for (<your for statement>) {
bulk.find({ID: <your id>, HASH: <your hash>}).upsert().update({<your update fields>});
}
bulk.execute(<your callback>)
For each document, it will look for a document matching the {ID: <your id>, HASH: {your hash}} criteria. Then:
If it finds one, it will update that document using {<your update fields>}
Otherwise, it will create a new document
As you need, it will not make a connection to the mongo server on each iteration of the for loop. Instead a single call will be made on the bulk.execute() line.

How to efficiently bulk insert and update mongodb document values from an array?

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

Resources