What is wrong with MongoDB remove query using mongodb java for sub-array of document?

人走茶凉 提交于 2019-12-11 23:20:48

问题


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);


来源:https://stackoverflow.com/questions/29600055/what-is-wrong-with-mongodb-remove-query-using-mongodb-java-for-sub-array-of-docu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!