简体   繁体   English

如何在mongo中更新嵌入式文档?

[英]How to update embedded document in mongo?

I am having the following document in the mongo db. 我在mongo数据库中有以下文档。

  { "_id" : ObjectId("50656f33a4e82d3f98291eff"), 
     "description" : "gdfgdfgdfg",
     "menus" : [    
               {    
                 "name" : "gdfgdfgdfg",     
                  "description" : "dfgdgd",     
                  "text" : "dfgdfg",    
                  "key" : "2",  
                  "onSelect" : "yyy",   
                  "_id" : ObjectId("50656f3ca4e82d3f98291f00") 
              }, 
               {    
                 "name" : "rtytry",     
                  "description" : "gffhf",  
                  "text" : "dfgdfg",    
                  "key" : "2",  
                  "onSelect" : "yyy",   
                  "_id" : ObjectId("50656f3ca4e82d3f98281f00") 
              }],
      "select":"ffdfgd"
   }

I want to do automatic update of menus { 我想自动更新菜单{
"name" : "gdfgdfgdfg", “ name”:“ gdfgdfgdfg”,
"description" : "dfgdgd", “ description”:“ dfgdgd”,
"text" : "dfgdfg", “ text”:“ dfgdfg”,
"key" : "2", “ key”:“ 2”,
"onSelect" : "yyy", “ onSelect”:“ yyy”,
"_id" : ObjectId("50656f3ca4e82d3f98291f00") } “ _id”:ObjectId(“ 50656f3ca4e82d3f98291f00”)}

I have tried using the following code: 我尝试使用以下代码:

         BasicDBObject query = new BasicDBObject("_id", new ObjectId("50656f33a4e82d3f98291eff"));

         BasicDBObject update = new BasicDBObject("_id", ObjectId("50656f3ca4e82d3f98291f00"));

         BasicDBObject updateCommand = new BasicDBObject("$set", new BasicDBObject("menus", update));

         collection.update(query, updateCommand);

The result i got is 我得到的结果是

  { "_id" : ObjectId("50656f33a4e82d3f98291eff"), 
 "description" : "gdfgdfgdfg",
 "menus" :    
           {    
             "name" : "gdfgdfgdfg",     
              "description" : "dfgdgd",     
              "text" : "dfgdfg",    
              "key" : "2",  
              "onSelect" : "yyy",   
              "_id" : ObjectId("50656f3ca4e82d3f98291f00") 
          },
  "select":"ffdfgd"

} }

but i want to update in the same embedded document. 但我想在同一嵌入式文档中进行更新。

Any one guide me ....Thanks in advance 任何人都可以引导我....提前感谢

Ok After having to read this a couple of times (English...) I think I understand now. 好的在读了几次(英语...)之后,我想我现在明白了。

You want to upto the subdocument with the _id 50656f3ca4e82d3f98291f00 with: 您要使用_id 50656f3ca4e82d3f98291f00子文档:

{
"name" : "gdfgdfgdfg",
"description" : "dfgdgd",
"text" : "dfgdfg",
"key" : "2",
"onSelect" : "yyy",
"_id" : ObjectId("50656f3ca4e82d3f98291f00") 
} 

First problem you have is that your code merely sets menus field to this object. 您遇到的第一个问题是您的代码仅将menus字段设置为此对象。 What you need is the postional operator. 您需要的是位置运算符。 So you need to do a dot notation find (warning my Java is a little rusty): 因此,您需要进行点符号查找(警告我的Java有点生锈):

query.append("menus._id", new ObjectId("50656f3ca4e82d3f98291f00"));

And then use that positional operator to update in position: 然后使用该位置运算符更新位置:

BasicDBObject update_document = new BasicDBObject("menus.$.name", my_new_subdocument.name);
update_document.append("menus.$.description", my_new_subdocument.description);
// All the other fields

BasicDBObject updateCommand = new BasicDBObject("$set", update_document);

Hopefully that should do the trick. 希望这可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用spring mongo data api更新嵌入式mongo文档 - How to update embedded mongo document using spring mongo data api 如何更新具有复杂结构的Mongo DB文档 - How to update Mongo DB document with complex structure 如何编写查询以更新嵌入式文档 - How to write query for update embedded document Mongo DB将不会更新现有文档 - Mongo db will not update an existing Document 如何在mongo中构造查询以更新嵌套数组文档? - How to construct query to update nested array document in mongo? 如何在不丢失 Spring Mongotemplate 项目中的数据的情况下更新 Mongo 中的文档 - How to update document in Mongo without loosing data in a Spring Mongotemplate project 如何使用 OpenOffice Java API 更新 word 文档中嵌入的 Excel 表? - How to update an embedded Excel table in a word document with OpenOffice Java API? 是否可以在mongo文档的嵌入式数组文档中添加对象列表? - Is it possible to add list of objects into embedded array document in mongo document? 如何获得具有匹配条件的mongo结果以及嵌入式文档数组中的ceratain id的验证 - How to get mongo results with matching condition along with verification of ceratain id in an embedded document array 如何使用java从mongo db中的数组中嵌入的内部文档中检索数据 - How to retrieve data from inner document embedded in array in mongo db using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM