Mongodb:查询嵌套在数组中的 json 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24294273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mongodb: Query a json-object nested in an array
提问by Jonas M.
I'm quite new to mongodb and there is one thing I can't solve right now:
Let's pretend, you have the following document (simplified):
我对 mongodb 很陌生,我现在无法解决一件事:
假设您有以下文档(简化版):
{
'someKey': 'someValue',
'array' : [
{'name' : 'test1',
'value': 'value1'
},
{'name' : 'test2',
'value': 'value2'
}
]
}
Which query would return the json-object, in which the value equals 'value2'?
哪个查询会返回 json 对象,其中值等于“value2”?
That means, i need this json-object:
这意味着,我需要这个 json 对象:
{
'name' : 'test2',
'value': 'value2'
}
Of course I already tried a lot of possible queries, but none of them returned the right, e.g.
当然,我已经尝试了很多可能的查询,但没有一个返回正确的,例如
db.test.find({'array.value':'value2'})
db.test.find({'array.value':'value2'}, {'array.value':1})
db.test.find({'array.value':'value2'}, {'array.value':'value2'})
Can someone help and show me, what I'm doing wrong?
Thanks!
有人可以帮助并告诉我,我做错了什么吗?
谢谢!
回答by Tushar Mishra
Using Positional operator
使用位置运算符
db.test.find(
{ "array.value": "value2" },
{ "array.$": 1, _id : 0 }
)
Output
输出
{ "array" : [ { "name" : "test2", "value" : "value2" } ] }
Using aggregation
使用聚合
db.test.aggregate([
{ $unwind : "$array"},
{ $match : {"array.value" : "value2"}},
{ $project : { _id : 0, array : 1}}
])
output
输出
{ "array" : { "name" : "test2", "value" : "value2" } }
Using Java Driver
使用 Java 驱动程序
MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 27017));
DB db = mongoClient.getDB("mydb");
DBCollection collection = db.getCollection("test");
DBObject unwind = new BasicDBObject("$unwind", "$array");
DBObject match = new BasicDBObject("$match", new BasicDBObject(
"array.value", "value2"));
DBObject project = new BasicDBObject("$project", new BasicDBObject(
"_id", 0).append("array", 1));
List<DBObject> pipeline = Arrays.asList(unwind, match, project);
AggregationOutput output = collection.aggregate(pipeline);
Iterable<DBObject> results = output.results();
for (DBObject result : results) {
System.out.println(result.get("array"));
}
output
输出
{ "name" : "test2" , "value" : "value2"}
回答by V_Singh
Try the $in operator like this:
像这样尝试 $in 运算符:
db.test.find({"array.value" : { $in : ["value2"]}})
db.test.find({"array.value" : { $in : ["value2"]}})
回答by IGDEV
You can pass multiple find objects in element match for getting the exact answer.
您可以在元素匹配中传递多个查找对象以获得确切答案。
db.test.find({'array':{$elemMatch:{value:"value2"}})
output: {'name' : 'test1','value': 'value1'}
回答by KARTHIKEYAN.A
Use $elemMatchand dot(.) to get your required output
使用$elemMatch和 dot(.) 获得所需的输出
db.getCollection('mobiledashboards').find({"_id": ObjectId("58c7da2adaa8d031ea699fff") },{ viewData: { $elemMatch : { "widgetData.widget.title" : "England" }}})

