OTUser
OTUser

Reputation: 3848

Mongodo db delete documents between custom fields

I have below documents in mongodb, am trying to delete the documents based on the referenceId field between the values X0000000005 and X00000000010, I couldnt find any articles for deleting mongo documents based on custom field, can someone please help me to do this deletion if its possible?

{
    "_id" : ObjectId("5a0f13ad0a83924b84d16b7d"),    
    "senderId" : "783",
    "clientId" : "146196",
    "referenceId" : "X00000000001",    
    "file" : "jAAAAAECAAABaAAAAKQAAJyMKYqPYvFQKJrZ/fqYjDKNdXdOMK58tPQ"
}

{
    "_id" : ObjectId("5a0f13ad0a83924b84d16b7e"),    
    "senderId" : "783",
    "clientId" : "146196",
    "referenceId" : "X00000000002",    
    "file" : "jAAAAAECAAABaAAAAKQAAJyMKYqPYvFQKJrZ/fqYjDKNdXdOMK58tPQ"
}
.
.
.
.
.
.
{
    "_id" : ObjectId("5a0f13ad0a83924b84d16b7f"),    
    "senderId" : "783",
    "clientId" : "146196",
    "referenceId" : "X00000000020",    
    "file" : "jAAAAAECAAABaAAAAKQAAJyMKYqPYvFQKJrZ/fqYjDKNdXdOMK58tPQ"
}

Upvotes: 1

Views: 42

Answers (2)

Matt
Matt

Reputation: 1196

db.collection.remove({"referenceId": {"$lte": "X00000000010", "gte": "X0000000005"}})

Upvotes: 1

dnickless
dnickless

Reputation: 10918

The following simple query should work:

db.collection.remove({"referenceId":{$gte:"X00000000005"}, "referenceId":{$lte:"X00000000010"}})

You might want to run a find() using the same filter first in order to make sure that the delete() will affect the right records. That'd obviously be this then:

db.collection.find({"referenceId":{$gte:"X00000000005"}, "referenceId":{$lte:"X00000000010"}})

Also, depending on the exact definition of your

between the values X0000000005 and X00000000010

you might need to swap the $lte and $gte operators out for something else ($gt and/or $lt).

Upvotes: 2

Related Questions