[JAVA-1418] Java Driver can not update embedded array document Created: 01/Sep/14  Updated: 01/Sep/14  Resolved: 01/Sep/14

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: 2.12.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Tao Lin Assignee: Ross Lawley
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

this command:
db.collection.update(

{ <query selector> }

, { <update operator>:

{ "array.$.field" : value }

} )
use array.$.field" as BasicDBObject key, the exception say:

Exception in thread "main" java.lang.IllegalArgumentException: Document field names can't have a . in them. (Bad Key: 'dimensionArray.$.v')
at com.mongodb.DBCollection.validateKey(DBCollection.java:1843)
at com.mongodb.DBCollection._checkKeys(DBCollection.java:1803)
at com.mongodb.DBCollection._checkObject(DBCollection.java:1790)
at com.mongodb.DBCollectionImpl.update(DBCollectionImpl.java:256)
at com.mongodb.DBCollection.update(DBCollection.java:191)
at com.mongodb.DBCollection.update(DBCollection.java:224)



 Comments   
Comment by Tao Lin [ 01/Sep/14 ]

It's my mistake, I loss the operator "$set", Please close this issue.thanks for your reply

Comment by Ross Lawley [ 01/Sep/14 ]

Hi bingyunxl a field key cannot contain a `.` so it looks like something is amiss with your update.

The documentation has an example of Updating documents in an array which in Java looks like:

        BasicDBList grades = new BasicDBList();
        grades.add(new BasicDBObject("grade", 80).append("mean", 75).append("std", 8));
        grades.add(new BasicDBObject("grade", 85).append("mean", 90).append("std", 5));
        grades.add(new BasicDBObject("grade", 90).append("mean", 85).append("std", 3));
 
        collection.save(new BasicDBObject("_id", 4).append("grades", grades));
 
        collection.update( new BasicDBObject("_id", 4).append("grades.grade", 85),
                           new BasicDBObject("$set", new BasicDBObject("grades.$.std", 6)) );
 
        System.out.println(collection.find(new BasicDBObject("_id", 4)));

Can you provide an example of your java code?

Generated at Thu Feb 08 08:54:35 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.