简体   繁体   中英

Modify nest document's value in MongoDB for Java

A very quick question, how am I going to do this below:

> db.blog.posts.findOne()
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"title" : "A Blog Post",
"content" : "...",
"author" : {
    "name" : "joe",
    "email" : "joe@example.com"
     }
}

I saw the answer in Javascript is like:

> db.blog.posts.update({"author.name" : "joe"}, {"$set" : {"author.name" : "joe schmoe"}})

But how am I going to do that in Java?

If I have a very deep level value has to be changed, am I supposed to use this way? like: "person.abc.xyz.name.address" ?

Using dot notation to access nested documents will work perfectly well in the Java Driver. Take a look at this StackOverflow answer:

MongoDB nested documents searching

For the Java Driver, the basic idea is to replace the Javascript objects with instances of BasicDBObject.

Here's another good reference for updating:

MongoDb's $set equivalent in its java Driver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM