带有$ project的聚合仅匹配一个元素
我是使用MongoDB的初学者,在互联网上搜索了很多之后,我发现没有什么可以帮助您的。我想检索以下查询的实际内容,但要查询集合中的所有文档。
我要实现的是从rating.views中检索元素,并为其指定“ from”和“ to”参数
rBusiness文档:
{
"providerId" : ObjectId("5b29313f7d260b6cb8da14c2"),
"rating" : {
"views" : [
ISODate("2017-07-02T00:00:00.000Z"),
ISODate("2017-07-02T00:00:00.000Z"),
ISODate("2018-07-02T00:00:00.000Z"),
ISODate("2018-07-02T00:00:00.000Z"),
ISODate("2018-07-03T00:00:00.000Z"),
ISODate("2019-07-03T00:00:00.000Z"),
ISODate("2019-07-03T00:00:00.000Z"),
ISODate("2019-07-03T00:00:00.000Z")
]
}
}
Mongo查询:
db.getCollection('rBusiness').aggregate([
{
$project: {
'rating.views': {
$filter: {
input: "$rating.views",
as: "views",
cond: { $and: [
{ $gte: [ "$$views", ISODate("2018-01-01T00:00:00.000Z") ] },
{ $lte: [ "$$views", ISODate("2018-12-25T00:00:00.000Z") ] }
]}
}
}
}
},
{ $match: { providerId: ObjectId("5b439e54c816cd018a5b7ad2") } }
])
我当前查询得到了什么:
Error: command failed: {
"ok" : 0,
"errmsg" : "input to $filter must be an array not int",
"code" : 28651,
"codeName" : "Location28651"
}
我希望检索的内容(文档列表):
{
"_id" : ObjectId("5b439e54c816cd018a5b7ad2"),
"rating" : {
"views" : [
ISODate("2018-07-02T03:21:20.201Z"),
ISODate("2018-07-02T03:21:20.201Z"),
ISODate("2018-07-03T03:22:20.201Z")
]
}
},
{
"_id" : ObjectId("5b439e54c816cd018a5b7ad3"),
"rating" : {
"views" : [
ISODate("2018-07-02T03:21:20.201Z"),
ISODate("2018-07-02T03:21:20.201Z"),
ISODate("2018-07-03T03:22:20.201Z")
]
}
}