Qaz
Qaz

Reputation: 1576

MongoDB Remove All Rows with a Certain Field

How can I remove all rows/records/entries that have a certain property set in MongoDB? For example, how can I remove all the rows that have an x field without removing the last row, which has no x field?

{ "_id" : ObjectId("53907a0adf55a0a97263b36d"), "x" : 21 }
{ "_id" : ObjectId("53907a0adf55a0a97263b36e"), "x" : 22 }
{ "_id" : ObjectId("53907a0adf55a0a97263b36f"), "x" : 23 }
{ "_id" : ObjectId("53907a0adf55a0a97263b370"), "x" : 24 }
{ "_id" : ObjectId("53907a16df55a0a97263b372"), "name" : "Bob" }

I tried this, but it removed everything:

db.testData.remove({}, {x:""})

Upvotes: 1

Views: 1047

Answers (1)

kwolfe
kwolfe

Reputation: 1683

http://docs.mongodb.org/manual/reference/operator/query/exists/

db.testData.remove({x: {$exists: true}})

Upvotes: 3

Related Questions