0

I want to modify array element using Java MongoDB driver. I am able to insert new pair to the array, but can't modify value corresponding to particular key.

How can I increase a by 2 in dummy array for document

{ "_id" : ObjectId("57a87614d03a435e4be44bb9"), "dummy" : [ { "a" : 1 }, { "b" : 5 } ] }

using Java MongoDB driver?

Here is what I've tried

BasicDBObject query = new BasicDBObject();
query.put("_id",doc_id_here); 
BasicDBObject incValue = new BasicDBObject("dummy.$.a", 1);
BasicDBObject intModifier = new BasicDBObject("$inc", incValue); 
coll.update(query, intModifier, false, false, WriteConcern.SAFE);
3
  • {"_id" : ObjectId("57a87614d03a435e4be44bb9"), "dummy" : [ { "a" : 1 }, { "b" : 5 } ]} Commented Aug 8, 2016 at 16:01
  • show what have you already tried by adding Java code to your question Commented Aug 8, 2016 at 16:13
  • BasicDBObject query = new BasicDBObject(); query.put("_id",doc_id_here); BasicDBObject incValue = new BasicDBObject("dummy.$.a", 1); BasicDBObject intModifier = new BasicDBObject("$inc", incValue); coll.update(query, intModifier, false, false, WriteConcern.SAFE); Commented Aug 8, 2016 at 18:14

1 Answer 1

0

Your query in mongo shell

db.collection.update( 
  { "_id": ObjectId("57a87614d03a435e4be44bb9") }, 
  { $inc: { "dummy.$.a": 1 } } 
);

will result in error

The positional operator did not find the match needed from the query. Unexpanded update: dummy.$.a

because in order to use positional $ operator for dummy array you need to set condition on this array in your query

db.collection.update( 
  { "_id": ObjectId("57a87614d03a435e4be44bb9"), "dummy.a": { $exists: true } }, 
  { $inc: { "dummy.$.a": 1 } } 
);

and then it will increment a as you expect.

With Java MongoDB driver it will be

BasicDBObject query = new BasicDBObject();
query.put("_id", new ObjectId("57a87614d03a435e4be44bb9"));
query.put("dummy.a", new BasicDBObject("$exists", true));
BasicDBObject incValue = new BasicDBObject("dummy.$.a", 1);
BasicDBObject intModifier = new BasicDBObject("$inc", incValue); 
coll.update(query, intModifier, false, false, WriteConcern.SAFE);

Advice: before trying to construct your query with Java MongoDB Driver first try if it works in mongo shell.

Sign up to request clarification or add additional context in comments.

3 Comments

i have one more doubt.. suppose key is not exist in array then i want to insert key value.. if key exist then value increment by 1... For Example. key "a" exist so i will increment value corresponding to key a by 1. case to key "f" not exist in array so i want insert in dummy array {f:1}
This is another issue, I am not sure you can complete it with 1 query. Check here. If solution with 2 queries (where first query will search document with corresponding key and second query will update your collection according to the result of first) works for you, then update this question or create another one. I am not going to edit your question for the third time, please, do it yourself.
Thanks for quick reply.. i can do it in 2 query but i wanted in one query.. let me check

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.