0

I have a Spring project with MongoDB. I don't have a domain class because my project treats data in a generic way. I'm trying to delete some objects that I have inside an array but I can't. I've already searched StackOverflow but the information found didn't help me fix this. Here is a summarized example of a Document in my Collection:

{ _id
  dispositivos:[
            { uid },
            { uid }
}

I try to delete only the objects that have the id that I passed in the list of ids. This is what I have tried so far, but it doesn't work.

    public UpdateResult deleteDispositivosByIds(String idCentro, List<String> ids) {
        // NOT WORKING--------
        
//      ObjectId objectId = new ObjectId(idCentro);
//      Query query = new Query();
//      query.addCriteria(Criteria.where("_id").is(objectId));
//      Update update = new Update();
//      BasicDBObject[] boArray = new BasicDBObject[ids.size()];
//      String[] idsArray = new String[ids.size()];
//      idsArray=ids.toArray(idsArray);
//      for (int i = 0; i < boArray.length; i++) {
//          boArray[i]=new BasicDBObject("uid",idsArray[i]);
//          System.out.println(idsArray[i]);
//        }
//      update.pullAll("dispositivos",boArray );            
//      mongoTemplate.updateFirst(query, update, "centros");
//      return null;
        
        // NOT WORKING--------
        
//      ObjectId objectId = new ObjectId(idCentro);
//      Query query = new Query();
//      query.addCriteria(Criteria.where("_id").is(objectId));
//      Update update = new Update();       
//      update.pullAll("dispositivos",ids.toArray() );          
//      mongoTemplate.updateFirst(query, update, "centros");
//      return null;
        
        // NOT WORKING--------
        
        ObjectId objectId = new ObjectId(idCentro);
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(objectId));
        query.addCriteria(Criteria.where("dispositivos.uid").in(ids));
        Update update = new Update();       
        update.pull("dispositivos", query );            
        mongoTemplate.updateFirst(new Query(), update, "centros");
        return null;
    }
1

1 Answer 1

0

that was my solution ( "centros" is the collection name):

    public UpdateResult deleteDispositivosByIds(String idCentro, List<String> ids) {

        
        ObjectId objectId = new ObjectId(idCentro);
        
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(objectId));
        
        Update update = new Update();       
        update.pull("dispositivos", new Query(Criteria.where("uid").in(ids)) );
        
        return mongoTemplate.updateFirst(query, update, "centros");     
        
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.