4

So I have object:

"_id" : 1,
"employee_id" : [2, 3, 4, 5],
"project_name" : "qwerty"

And I want to delete from "employee_id" array [3, 5] and add new array [13, 6, 8]. And result will be:

"_id" : 1,
"employee_id" : [2, 4, 13, 6, 8],
"project_name" : "qwerty"

I use this Java-code:

DB database = mongoClient.getDB("employee_service");
DBCollection collectionProject = database.getCollection("project");

DBObject query = new BasicDBObject();
query.put("_id", project.getId());

DBObject projectMongoObject = new BasicDBObject();
projectMongoObject.put("project_name", project.getProjectName());

//something
collectionProject.update(query, projectMongoObject);

So how to set in projectMongoObject new array and delete array?

3
  • Have you tried requesting the existing array and updating with the extended array through overwriting? Commented Dec 3, 2014 at 8:05
  • @Smutje, no. But if I understand correctly, I will twitch database for: select array, add to array, delete from array, update project name. Is this a good idea? Commented Dec 3, 2014 at 8:10
  • No, just request the DB for the existing array, extend the object and request the DB to update the object containing the array. Commented Dec 3, 2014 at 8:11

1 Answer 1

2

Make use of the $pullAll operator to remove the fields, and the combination of $push and $each to add new fields to the array.

   DBObject query = new BasicDBObject();
   query.put("_id", project.getId());   
   DBObject projectMongoObject = new BasicDBObject();
   projectMongoObject.put("$set", new BasicDBObject("project_name",
                                                     project.getProjectName()));
   projectMongoObject.put("$pullAll", 
                          new BasicDBObject("employee_id", new int[]{3,5}));
   collectionProject.update(query, projectMongoObject);
   projectMongoObject = new BasicDBObject();
   projectMongoObject.put("$push", 
                            new BasicDBObject("employee_id",
                                              new BasicDBObject("$each",
                                                            new int[]{13,6,8})));
   collectionProject.update(query, projectMongoObject);
Sign up to request clarification or add additional context in comments.

6 Comments

Can I add and delete element's from array use 1 request to database?
No you cannot do that. Currently its a limitation in MongoDB.
One Minute, you can two fields at a time, that is possible, at the same time you cannot do two different operations on an single field. See my updated answer. Have edited to show that you can add elements to an array at the same time set the project_name field as well.
I have some problem: my new project_name value doesn't saving in database when I use "$set"
Are you using the $set operator to set the value for the field? Are you getting any errors?
|

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.