0

I'm trying to delete an object from an array of objects in mongodb collection. I've followed multiple answers from SO but I couldn't able to make it work.

customer_id and address_pin will be passed from querystring. I want to remove an object from address_india where address_pin matches with the value passed via querystring.

MongoDB collection object

{
   "_id":{
      "$oid":"61fa7de3b3de485b30403c56"
   },
   "customer_id":"c7e690cf-c8d9-4a7b-b146-ade742958452",
   "customer_addresses":{
      "address_india":[
         {
            "address_id":"ccc428bd-3e4d-49e5-8569-ebacb181ad1e",
            "address_pin":"MD0BuCQUxT",  // I've to remove this object          
            "address_type":"sales"
         },
         {
            "address_id":"ccc428bd-3e4d-49e5-8569-ebacb181asdf",
            "address_pin":"MD0BuCQUXy",            
            "address_type":"marketing"
         }
      ]
   },
   "create_timestamp":{
      "$numberLong":"1643806179346"
   },
   "modified_timestamp":{
      "$numberLong":"1643806179346"
   }
}

Controller

return new ResponseEntity<CustomerAddressResponse>(
                this.customerAddressService.deleteCustomerAddressById(customerId),
                HandleResponseHeader.getResponseHeaders(UUID), HttpStatus.OK);

ServiceImpl

@Override
    public CustomerAddressResponse deleteCustomerAddressById(String customerId)
            throws Exception {

        try {
            if (isValidUUID(customerId)) {
                Date now = new Date();
                Long dateStartTime = now.getTime();
                Stopwatch stopWatch = Stopwatch.createStarted();
                CustomerAddress customerAddress = customerAddressDao
                        .findByCustomerId(customerId);
                String pin = httpServletRequest.getParameter("address_pin")
                        .trim();
                
                Query query = new Query();
                query.addCriteria(Criteria.where("customer_id")
                        .is(customerId)
                        .and("customer_addresses.address_india")
                        .elemMatch(Criteria.where("address_pin").is(pin)));
                
                Update update = new Update();
                update.pull("customer_addresses.address_india", new Query().addCriteria(Criteria.where("address_pin").is(pin)));
                FindAndModifyOptions options = FindAndModifyOptions.options();
                options.returnNew(true);
                MongoTemplate template = null;
                template.findAndModify(query, update, options, CustomerAddress.class);
                }
            }
        }

I'm getting java.lang.NullPointerException at template.findAndModify(query, update, options, CustomerAddress.class);

How do I delete an object from array of objects in mongodb from springboot?

1 Answer 1

1

This is how you do from mongo js shell for single document , it may give you some idea on how to do from springboot:

db.collection.update(
 {
  "customer_id": "c7e690cf-c8d9-4a7b-b146-ade742958452"},
 {
   $pull: {
   "customer_addresses.address_india": {
   "address_pin": "MD0BuCQUxT"
   }
 }
})

playground

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.