mongodb数组指定项删除
比如:{"_id":ObjectId("534673c4e2632e93652fb70b"),"area":"5346603fe2630f5cd4748abd","com...
比如:{ "_id" : ObjectId("534673c4e2632e93652fb70b"), "area" : "5346603fe2630f5cd4748abd", "company" : "533e226da96d1c617a02b365", "desc" : "b", "name" : "b", "owners" : [ { "id" : ObjectId("534511bce263980b2f26fafe"), "enable" : true }, { "id" : ObjectId("533e3ba8a96d27855e057994"), "enable" : true } ] } 根据 _id,company,owners.id,删除指定的owners.id项。我是这样写的:
BasicDBObject query=new BasicDBObject();
query.put("_id", new ObjectId(did));
query.put("company",cid);
query.put("owners.id",new ObjectId(uid));
BasicDBObject _child=new BasicDBObject();
BasicDBObject child=new BasicDBObject();
child.put("id",new ObjectId(uid));
child.put("enable",true);
_child.put("owners",child);
try {
WriteResult r=DBManagerMongoDB.getInstance().Update("DataCenter", query,new BasicDBObject("$pull",_child),true,false);
if(r.getN()>-1){
flag=true;
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
return flag;
}
新建一条数据吧。明明指定了条件,可是每次删除的时候,都是删除owner index最后一条数据,本应该删除指定的那一项。求怎么才能实现。。。 展开
BasicDBObject query=new BasicDBObject();
query.put("_id", new ObjectId(did));
query.put("company",cid);
query.put("owners.id",new ObjectId(uid));
BasicDBObject _child=new BasicDBObject();
BasicDBObject child=new BasicDBObject();
child.put("id",new ObjectId(uid));
child.put("enable",true);
_child.put("owners",child);
try {
WriteResult r=DBManagerMongoDB.getInstance().Update("DataCenter", query,new BasicDBObject("$pull",_child),true,false);
if(r.getN()>-1){
flag=true;
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
return flag;
}
新建一条数据吧。明明指定了条件,可是每次删除的时候,都是删除owner index最后一条数据,本应该删除指定的那一项。求怎么才能实现。。。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询