top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

MongoDB: How to remove documents from a collection which meet multi-key combination deletion criteria ?

+3 votes
355 views

Here I would like to explain my query a bit more.

I inserted multiple documents within a collection. Few documents have same set of keys and few of them have some extra key:value pairs . For example:
document 1 : key1 : "key1"
document 2: key1 : "key1", key2: "key2"
document 3: key1 : "key1", key2: "key2", key3 : "key3"
document 4: key1 : "key1", key3 : "key3"

Now I want to delete the documents which have key1 and key2.
What will be the command to do so ?

posted Jun 26, 2015 by Harshita

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

It is pretty simple query. Use the following command.
db.collection_name.remove({selection criteria})

Assume, there is a collection named as "myCollection". And the collection holds multiple records/documents like that.
document 1 has following key:value pairs - key1: 2, key2: 4, key4:5
document 2 has following key: value pairs - key1: 2, key4:6, key5:6
document 3 has following key: value pairs - key1:2, key2:4 , key6 : 7

if you want to delete documents which have key1;2 and key2:4 then use command in such a way.
db.myCollection.remove({key1:2, key2:4})

The above command will delete document 1 and document 3 from "myCollection" .

answer Jun 27, 2015 by Vikram Singh
Similar Questions
+1 vote

Does anyone know how I remove orphaned documents in a cluster (sharding) in mongodb version 3.0.10, My actual data are 20 million orphans and documents the value amounts to 21 million data

+1 vote

I have a project, which has 1000 images. From each image, I get 1 million documents that I currently store in a mongoDB collection.

I have 2 alternatives,
1. Create a separate collection for each image, over multiple projects, say 10 projects, and each image(collection) would have 1 million documents that will be indexed. OR
2. Have a single collection and have 1000x1,000,000 documents in one collection, which again will be indexed.

My research and reading suggests that it is not a very good idea to create 1000 collections (for each image) and create an index for each one of 1000, and go with adding a billion documents to a single collection, to which mongoDB caters with no major issue.
My main aim is to enhance performance for reading. There would be no updates and deletes. Only reads and writes. I can afford writes to be slow, but not reads. So am I right in choosing the 2nd alternative?

Any guidance is highly appreciated.

...