问题描述
如果要匹配给定条件,我想从数组中投影所有对象.
I want to project all the objects from an array, if it matches the given condition.
我有以下数据
{
_id : 1,
em : '[email protected]',
name : 'NewName',
od :
[
{
"oid" : ObjectId("1234"),
"ca" : ISODate("2016-05-05T13:20:10.718Z")
},
{
"oid" : ObjectId("2345"),
"ca" : ISODate("2016-05-11T13:20:10.718Z")
},
{
"oid" : ObjectId("57766"),
"ca" : ISODate("2016-05-13T13:20:10.718Z")
}
]
},
{
_id : 2,
em : '[email protected]',
name : 'NewName2',
od :
[
{
"oid" : ObjectId("1234"),
"ca" : ISODate("2016-05-11T13:20:10.718Z")
},
{
"oid" : ObjectId("2345"),
"ca" : ISODate("2016-05-12T13:20:10.718Z")
},
{
"oid" : ObjectId("57766"),
"ca" : ISODate("2016-05-05T13:20:10.718Z")
}
]
}
我想从od数组中获取所有对象,如果'od.ca'介于范围之内(如果大于等于5月且小于等于15日).
I want to get all the objects from od array, if 'od.ca' comes between range say, if greater than 10th may and less than 15th may.
我尝试使用mongodb的聚合方法,但对这种方法不熟悉.我的查询如下.
I tried using aggregate method of mongodb and I am new to this method. My query is as given below.
db.userDetail.aggregate(
{
$match:
{
'od.ca':
{
'$gte': '10/05/2016',
'$lte': '15/05/2016'
},
lo: { '$ne': 'd' }
}
},
{
$redact:
{
$cond:
{
if:
{
$gte: [ "$$od.ca", '10/05/2016' ],
$lte : ["$$od.ca" , '15/05/2016']
},
then: "$$DESCEND",
else: "$$PRUNE"
}
}
})
当我尝试使用此命令时,出现错误:-
When I am trying to use this command, getting error :-
由于我使用的是mongodb 3.0.0,因此无法使用 $ fiter .因此,我尝试使用 $ redact .
Since I am using mongodb 3.0.0 I can not use $fiter. So I tried using $redact.
有人可以告诉我我在做什么错吗?查询正确吗?
Can someone tell me what wrong I am doing? Is the query correct?
推荐答案
查询说明:
- $ match-为条件匹配文档-限制要处理的文档
- $ unwind-从输入文档中构造 od 数组字段,以输出每个元素的文档.每个输出文档都是输入文档,其中array字段的值被元素替换.
- $ match-为条件匹配文档
- $ group-在我们的例子中这与$ unwind相反-因此我们正在重新创建结果数组
- $match - match documents for criteria - limit documents to process
- $unwind - econstructs od array field from the input documents to output a document for each element. Each output document is the input document with the value of the array field replaced by the element.
- $match - match documents for criteria
- $group - this is opposite of $unwind in our case - so we are recreating results array
如果您希望获得这样的文档:
If you are expecting document like this:
{
"_id" : 2,
"od" : [{
"oid" : 1234,
"ca" : ISODate("2016-05-11T13:20:10.718Z")
}, {
"oid" : 2345,
"ca" : ISODate("2016-05-12T13:20:10.718Z")
}
]
}, {
"_id" : 1,
"od" : [{
"oid" : 2345,
"ca" : ISODate("2016-05-11T13:20:10.718Z")
}, {
"oid" : 57766,
"ca" : ISODate("2016-05-13T13:20:10.718Z")
}
]
}
您可以使用下面的查询:
you can use query bellow:
db.userDetail.aggregate([{
$match : {
"od.ca" : {
$lt : new Date(new Date().setDate(new Date().getDate() + 2)),
$gte : new Date(new Date().setDate(new Date().getDate() - 4))
}
}
}, {
$unwind : "$od"
}, {
$match : {
"od.ca" : {
$lt : new Date(new Date().setDate(new Date().getDate() + 2)),
$gte : new Date(new Date().setDate(new Date().getDate() - 4))
}
}
}, {
$group : {
_id : "$_id",
od : {
$push : "$od"
}
}
}
])
这篇关于如何使用mongodb 3.0.0检索数组内的对象(如果匹配给定条件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!