How to delete N documents based on any given field of that document? - mongodb

I intend to delete a large number of documents from a collection. My collection has more than a million documents. My idea is say, query for 10k documents based on a common field and delete all of them. I'm not sure how to get this done. Any help ?

You can do a deleteMany, see the mongodb documentation

Related

query in mongodb atlas to verify the existence of multiple specific documents in a collection

I have a mongodb collection called employeeInformation, in which I have two documents:
{"name1":"tutorial1"}, {"name2":"tutorial2"}
When I do db.employeeInformation.find(), I get both these documents displayed. My question is - is there a query that I can run to confirm that the collection contains only those two specified documents? I tried db.employeeInformation.find({"name1":"tutorial1"}, {"name2":"tutorial2"}) but I only got the id corresponding to the first object with key "name1". I know it's easy to do here with 2 documents just by seeing the results of .find(), but I want to ensure that in a situation where I insert multiple (100's) of documents into the collection, I have a way of verifying that the collection contains all and only those 100 documents (note I will always have the objects themselves as text). Ideally this query should work in mongoatlas console/interface as well.
db.collection.count()
will give you number of inserts once you have inserted the document.
Thanks,
Neha

MongoDB Fillter the records and Updating vs Updating with filters

If i want to update multiple documents based on multiple filter criteria which is the better approach?
Filter and get the documents (only _id field) which needs to be updated and supply the array of _id to updatemanyasync ($in) as a parameter and update . (see below 1)
Update the documents by supplying filter criteria directly.(see below 2)
Reason for this doubt.
1. MongoDB search only for _id matches and update it.
2. MongoDB search for the supplied mulitple criteria (multiple fields) each document and it will update.
What is the performance difference on these 2 approaches by spliting up the updates as 2 process
Performance on Timeouts,Locks,Document Avalability after update.
Please help to share your suggestions and views.

Get a list of records from a collection sorted by count and uniqueness of a field

So I have a bunch of documents in a MongoDB collection and it seems that the collection is growing a little faster than we thought.
Is there a way to get a list from a collection that will count the number of documents that have X as a value in a field.
For example(I'll just make data up)
there are 4 values possible for the field (reference).
/content/public
/content/private
/resource/something
/much/wow
Is there a way to get a list from mongo that says:
1231 Records have /content/public as the value for reference.
21312312 have /content/private
34 have /resource/something
34242 have /much/wow
Use the aggregation tools for this. You haven't listed a language in your question, so here's the mongodb command directly. This assumes your collection is named 'urls'.
db.urls.aggregate([{$group: {_id:'$reference', total:{$sum:1} } }]);

Insert to top of collection in mongodb

Is it possible in MongoDB to insert a new document to the top of a collection? Normally, when creating new document, it is appended or added to the end of the collection.
Please shed some light
The order in which documents are returned from a collection is undefined behavior. Usually documents appear in the order they are inserted, but you can not rely on that. There are several circumstances which can cause the documents to get returned in a different order.
When you want documents in a specific order, you need to sort them explicitly when you search for them.

Remove given number of records in Mongodb

I have Too much records in my Collection, can I have only desired number of records and remove others without any condition?
I have a collection called Products with around 10,0000 of records and its slowing down my Local application, I am thinking to shrink this huge amount of records to something around 1000, How can do it?
OR
How to copy a collection with limited number of records?
If you want to copy collection with limited number of records without any filter condition, for loop can be used . It copies 1000 document form originalCollection to copyCollection.
db.originalCollection.find().limit(1000).forEach( function(doc)
{db.copyCollection.insert(doc)} );