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
Related
how to replace/update whole documents of particuar mongodb collection
eg: the process of save data in mongodb; call the api , the api get data from my website & save in mongodb.
for the first time I am calling the api, it gets data(5 items of places) from my website & should save in mongodb & collection length is 5 items.
after when I calling second time to the api, it gets data(now I am getting 17 items of places) the mongodb collections whole length should be 17(I am no need first api call data as 5 items of places & needs to remove those data)
I am using schema to figure out the data.
how to implement above case,
Although its not a good practice.But if you don't need you can remove all the existing documents before inserting new ones.
Try using remove query of mongodb.
Reference -- https://docs.mongodb.com/manual/reference/method/db.collection.remove/#mongodb-method-db.collection.remove
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.
I want to implement infinite-scrolling in my vueApp.
I'm using mongo, node, vue, mongoose and having problems to understand the logic on the server-side.
my issue is this:
let's say I have a collection with 3 million documents,
when the user is scrolling down he will get 5 documents every time.
how I'm implementing this into a mongo query that will send 5 documents each time and will know to proceed from the last record it sent once its called again?
Thanks!
You can try applying the logic applied to pagination, that is when the user reaches the down, increment the page number and pass it to the query to fetch new records!
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?
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/