What is wrong with MongoDB remove query using mong

2019-09-06 12:20发布

问题:

Remove in momgodb using java not giving proper result. Am i missing something? My database is as below.

My code for removeing records where index = "7" is as,

BasicDBObject whereQuery = new BasicDBObject("nodes.index", new BasicDBObject("$eq", "7"));
node_info.remove(whereQuery);

It's returning all the records and deleting my complete database. What can be probably wrong.?

回答1:

Instead of remove try $pull method code as below

BasicDBObject match = new BasicDBObject("_id", object id here);
BasicDBObject update = new BasicDBObject("nodes", new BasicDBObject("index", 7)))
node_info.update(match, new BasicDBObject("$pull", update);