我有一个包含元素
的集合会话{
"_id" : NumberLong(1),
"_class" : "cws.CWSession",
"title" : "java ",
"description" : "apprendre à programmer en java"
}
{
"_id" : NumberLong(2),
"_class" : "cws.CWSession",
"title" : "Git",
"description" : "Formation Git"
}
和我有另一个集合,其中包含CWSession的一个品种:
{
"_id" : NumberLong(2),
"_class" : "cws.CollaborativeWs",
"title" : "AngularJS",
"description" : "AngularJS",
"cws" : [
{
"$ref" : "cwsession",
"$id" : NumberLong(1)
},
{
"$ref" : "cwsession",
"$id" : NumberLong(2)
}
]
}
{
"_id" : NumberLong(3),
"_class" : "cws.CollaborativeWs",
"title" : "Java",
"description" : "principes java ",
"cws" : [
{
"$ref" : "cwsession",
"$id" : NumberLong(3)
}
]
}
我使用mongo模板删除会话
mongoTemplate.remove(new Query(Criteria.where("_id").is(id)), CWSession.class);
它工作得很好,会话在第一个集合中被删除,但在第二个集合中仍然存在,你能告诉我我可以用什么来删除第二个集合中的引用吗?提前谢谢你。
public void deleteSessionfromCW(String idCW, String idS){
Query query = new Query(where("_id").is(idCW).and("cws.id").is(id));
Update update = new Update().pull("cws", new BasicDBObject("id", "1"));
mongoTemplate.updateFirst(query, update, Item.class);
}
我有一个类似的问题,在我找到答案之前遇到了你未回答的问题。我将根据我为未来的谷歌人找到的答案发布一个解决方案
mongoTemplate.remove(new Query(Criteria.where("id").is(id)), CWSession.class);
mongoTemplate.remove(new Query(Criteria.where("cws.$id").is(new ObjectId(id)), CollaborativeWs.class);