Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Java pull

i tried to remove an embedded document without succcess. I'm looking for the java way of following instruction:

db.games.update({'_id': 73}, {$pull: {'goals': {'goal': 4}}})
like image 351
hehe Avatar asked Apr 05 '14 00:04

hehe


2 Answers

The Java documentation is pretty clear, you are just constructing BSON objects to match their respective JSON counterparts as used in the shell:

    BasicDBObject query = new BasicDBObject("_id", 73);
    BasicDBObject fields = new BasicDBObject("goals", 
        new BasicDBObject( "goal", 4));
    BasicDBObject update = new BasicDBObject("$pull",fields);

    games.update( query, update );
like image 114
Neil Lunn Avatar answered Nov 15 '22 04:11

Neil Lunn


Using Bson is similar.

Bson query = new Document().append("_id", 73);
Bson fields = new Document().append("goals", new Document().append( "goal", 4));
Bson update = new Document("$pull",fields);

games.updateOne( query, update );
like image 44
HenioJR Avatar answered Nov 15 '22 05:11

HenioJR