2

I have a collection session which contains elements

{
    "_id" : NumberLong(1),
    "_class" : "cws.CWSession",
    "title" : "java ",
    "description" : "apprendre à programmer en java"  
}

{
    "_id" : NumberLong(2),
    "_class" : "cws.CWSession",
    "title" : "Git",
    "description" : "Formation Git"    
}

And I have another collection which contain a dbRed of 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)
    }
]
}

I use mongo template to delete a session

mongoTemplate.remove(new Query(Criteria.where("_id").is(id)), CWSession.class);

It works fine and the session is deleted in the first collection but still in the second, can you please tell me what can I use to delete the reference in the second collection? And thank you in advance.

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);
}
Alyssa
  • 109
  • 12

1 Answers1

0

I had a similar problem and came across your unanswered question before I found my answer. I'll post a solution based on the answer I found for future Googlers 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); 
jamesidw
  • 66
  • 8