Mongodb Can';t使用数组元素获取聚合以返回结果
我有以下文件:Mongodb Can';t使用数组元素获取聚合以返回结果,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我有以下文件: { "_id" : ... "args" : { "pos" : [ <x>, <y> ], ... } } 我得到以下输出: { "result" : [ { "xmin" : [ ], "xmax" : [ ], "ymin" :
{
"_id" : ...
"args" : {
"pos" : [ <x>, <y> ],
...
}
}
我得到以下输出:
{
"result" : [
{
"xmin" : [ ],
"xmax" : [ ],
"ymin" : [ ],
"ymax" : [ ],
"hits" : 281
}
],
"ok" : 1
}
我尝试过各种不同的方法来获取这些信息和价值观,但我在这方面是新手,显然我遗漏了一些东西。任何帮助都将不胜感激
我想问题是我无法进入阵列。我尝试了以下更简单的查询,但也没有成功:
db.main.findOne({'function':'map'},{"arguments.pos":1})
{
"_id" : ObjectId("5110407a2c8bea0f0d0000ce"),
"arguments" : {
"pos" : [
-87.90774999999735,
42.11036897863933
]
}
}
db.main.findOne({'function':'map'},{"arguments.pos.0":1})
{
"_id" : ObjectId("5110407a2c8bea0f0d0000ce"),
"arguments" : {
"pos" : [ ]
}
}
db.main.findOne({'function':'map'},{"arguments.pos[0]":1})
{ "_id" : ObjectId("5110407a2c8bea0f0d0000ce"), "arguments" : { } }
如果有必要的话,我将从mongodb 2.2运行mongo shell。您不能在投影中使用
pos.0
语法。在find
中,您可以使用$slice
操作符,但这在$project
中
然而,你可以用另一种方式来做;使用$unwind
和另一个$group
提取x
和y
值:
db.main.aggregate([
{$match:{'args.pos':{$exists:true}},
{$unwind:'$args.pos'},
{$group:{
_id:“$\u id”,
x:{$first:'$args.pos'},
y:{$last:'$args.pos'}
}},
{$group:{
_id:null,
xmin:{$min:'$x'},
xmax:{$max:'$x'},
ymin:{$min:'$y'},
ymax:{$max:'$y'},
点击次数:{$sum:1}
}},
{$project:{{u id:0,xmin:1,xmax:1,ymin:1,ymax:1,hits:1}}
])
您不能在投影中使用pos.0
语法。在find
中,您可以使用$slice
操作符,但这在$project
中
然而,你可以用另一种方式来做;使用$unwind
和另一个$group
提取x
和y
值:
db.main.aggregate([
{$match:{'args.pos':{$exists:true}},
{$unwind:'$args.pos'},
{$group:{
_id:“$\u id”,
x:{$first:'$args.pos'},
y:{$last:'$args.pos'}
}},
{$group:{
_id:null,
xmin:{$min:'$x'},
xmax:{$max:'$x'},
ymin:{$min:'$y'},
ymax:{$max:'$y'},
点击次数:{$sum:1}
}},
{$project:{{u id:0,xmin:1,xmax:1,ymin:1,ymax:1,hits:1}}
])
非常感谢您。我花了几个小时寻找一种方法来做到这一点。每天学习新东西:)非常感谢。我花了几个小时寻找一种方法来做到这一点。每天学习新东西:)
db.main.findOne({'function':'map'},{"arguments.pos":1})
{
"_id" : ObjectId("5110407a2c8bea0f0d0000ce"),
"arguments" : {
"pos" : [
-87.90774999999735,
42.11036897863933
]
}
}
db.main.findOne({'function':'map'},{"arguments.pos.0":1})
{
"_id" : ObjectId("5110407a2c8bea0f0d0000ce"),
"arguments" : {
"pos" : [ ]
}
}
db.main.findOne({'function':'map'},{"arguments.pos[0]":1})
{ "_id" : ObjectId("5110407a2c8bea0f0d0000ce"), "arguments" : { } }