2

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!

3 Answers 3

2

You need to do the equivalent of:

db.usersCollection.update(
     {"_id":"yourId"}, 
     {$pull: 
            { "devices" : 
                    { $elemMatch : 
                        {
                          "guid":"local2", 
                          "state":"0"
                        }
                    }
            }
     }
)

Where you replace the yourId with the ID you want to query

That is, instead of using remove, use update, with $pull in the BasicDBObject. I can't test this right now but try:

DBCollection collection = db.getCollection(usersCollection);
BasicDBObject query = new BasicDBObject("_id", id);

BasicDBObject deviceToDelete = new BasicDBObject("guid",device);
deviceToDelete.append("state", "0");

BasicDBObject obj = new BasicDBObject("devices", deviceToDelete);
collection.update(query, new BasicDBObject("$pull",obj));

Let me know if this works (again I can't test because I don't have mongod on my machine).

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your help but this doesn't delete anything in the document :/
What is the value of device (the variable you're passing into the BasicDBObject)?
Is "local2", corresponding to guid field
Can you tell me the output of printing collection.findOne(query)?
I replace for it: db.usuario.findOne({devices:{"guid":"local2", "state":0}}) and get it : null
|
1

I fix the problem following this answer

This code works:

    DBCollection collection = db.getCollection(usersCollection);
    BasicDBObject match = new BasicDBObject("_id", id);
    BasicDBObject update2 = new BasicDBObject("devices", new BasicDBObject("guid", device)
                                      .append("state", 0));
    collection.update(match, new BasicDBObject("$pull", update2));

2 Comments

I'm sorry if I'm mistaken, but that seems to be exactly what I wrote, except you declared the BasicDBObject containing guid and state inside the update2 initialization, whereas I created the object and then put it into update2. Either way, it has an identical outcome. (Sorry if I missed something though, congrats on getting the answer!)
@Trust sorry but you answer was not useful to me.
1

You cannot use the remove-Function to remove a embeded document, you need to use update instead. This is the function call you need to implement in java:

db.userCollection.update(
{_id: ObjectId("53e53553b76000127cb1ab80")},
{$pull: 
    { devices : 
            {state: 0}
    }
})

Notice that the update-function takes to arguments, the first one to find the document and the second one to modify it.

DBCollection collection = db.getCollection(usersCollection);
BasicDBObject query = new BasicDBObject("_id", newObjectID("53e53553b76000127cb1ab80"));

BasicDBObject condition = new BasicDBObject("state",0);
BasicDBObject array = new BasicDBObject("devices", condition);
BasicDBObject update = new BasicDBObject("$pull", array);

collection.update(query, update);

I didn't test the Java Code but I tested the function call in mongoDB. If it doesn't work, check the implementation first.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.