我有一个包含元素的集合会话
{
"_id" : NumberLong(1),
"_class" : "cws.CWSession",
"title" : "java ",
"description" : "apprendre à programmer en java"
}
{
"_id" : NumberLong(2),
"_class" : "cws.CWSession",
"title" : "Git",
"description" : "Formation Git"
}
我有另一个包含CWRed的CWSed的集合:
{
"_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);
}
答案 0 :(得分:0)
我遇到了类似的问题,在找到答案之前遇到了未解答的问题。我会根据我为未来Google员工Other Question
找到的答案发布解决方案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);