I need delete all fields from the devices array they have the condition where state = 0
And this is my mongodb document:
{ "_id" : { "$oid" : "53e53553b76000127cb1ab80"} ,
"city" : "Some Place" ,
"devices" :
[ { "guid" : "local" , "brand" : "SSSS" , "state" : 1} ,
{ "guid" : "local2" , "brand" : "DDD" , "state" : 0} ,
{ "guid" : "local2" , "brand" : "DDD" , "state" : 0} ,
{ "guid" : "local2" , "brand" : "DDD" , "state" : 0}] ,
"phone": 8888888888,
"sex" : "Male"
}
This is my java code that I'm trying:
DBCollection collection = db.getCollection(usersCollection);
BasicDBObject query2 = new BasicDBObject("_id", id);
((BasicDBObject) query2).append("devices.guid", device);
((BasicDBObject) query2).append("devices.state", 0);
collection.remove(query2);
But this query delete all devices from the document. Thanks in advance!