2

要素を含むコレクション セッションがあります

{
    "_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 の dbRed を含む別のコレクションがあります。

{
"_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);

それは正常に動作し、セッションは最初のコレクションで削除されますが、それでも 2 番目のコレクションでは、2 番目のコレクションで参照を削除するために使用できるものを教えてください。そして、よろしくお願いします。

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

1 に答える 1