如何在mongoDB中获取内部文档属性的值? [英] How to get values of inner document attribute in mongoDB?
问题描述
我是mongoDB的新手.我在Web应用程序中使用mongoDB.
i am new to mongoDB. I am using mongoDB in web app.
我想知道如何获取文档的内部文档属性的值.
I want to know how to get the value of inner document attribute of a document.
让我们说我的JSON就像
Lets say my JSON is like,
{ "_id" : { "$oid" : "4ceb753a70fdf877ef5113ca"}, "Attrr1" : "value1", "Attr2": {"innerAttr1":"innerValue1", "innerAttr2":"innerValue2"}, "Attr3" : { "innerAttr3" : "innerValue3", "innerAttr4" : "innerValue4"} }
对于上述json,我该如何获取innerValue1
和innerValue1
和& innerValue2
.
For the above json, how can i get innerValue1
and both innerValue1
& innerValue2
.
我尝试过,但是对我不起作用!
I tried this, but its not working for me!!!
BasicDBObject innerQuery = new BasicDBObject();
field.put("Attr2.innerAttr1", 1);
任何建议都将是有意义的!
Any suggestions would be appreciative!!!
谢谢
推荐答案
我不明白你的意思.您需要获取价值或设置或添加到某些文档中的集合中吗?
i am not get you. You need get value or set or add into collection inside of some document?
据我了解,您在Java中为mongo db使用了一些驱动程序.我对Java的mongo驱动程序没有经验.但是我知道如何在驱动程序或c#中做到这一点:
As i understand you using some driver for mongo db in java. I am not experienced in mongo driver for java. But i know ho to do this in driver or c#:
在c#mongo驱动程序中,用于更新集合中的内部属性,您需要执行以下操作:
in c# mongo driver for update inner property in collection you need do following:
var query = Query.Eq("_id", documentId);
var update = Update.Set("Attr2.innerAttr1",1);
MongoDb.Database.GetCollection("collectionName").Update(query, update);
如果上述代码中的查询重新调整了一个文档-Attr2集合中的innerAttr1属性将被更新.如果我知道该查询返回一个以上的文档,并且我需要更新一个以上的文档,则可以使用以下代码:
if query from above code retun one document - innerAttr1 property from Attr2 collection will be update. If i know that query return more than one document and i need update more tha one document i use such code:
MongoDb.Database.GetCollection("collectionName").Update(query, update, UpdateFlag.Multi);
我相信mongo驱动程序中的Java逻辑相同.
I belive that the same logic in mongo driver for java.
希望获得帮助.
这篇关于如何在mongoDB中获取内部文档属性的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!