我在Mongo中有一个架构,如下所示:
{
"_id" : ObjectId("5c73cbeb2258414c5d9bf2a5"),
"make" : {
"name" : "Excel Boats",
"models" : [
{
"year" : 2012,
"name" : "1860V86",
"subModels" : [
{
"name" : "",
"controlNumber" : "OB1133079",
"protoTypeNumber" : "",
"status" : "A",
"classCode" : "OB",
"boatType" : "UT",
"powerType" : "OU",
"boatLength" : 18,
"horsePower" : 70,
"hullConstruction" : "A",
"msrp" : 9589,
"lowCostAmount" : 4700,
"highCostAmount" : 5270,
"retailAmount" : 6540,
"notes" : ""
},
{
"name" : "",
"controlNumber" : "OB1133080",
"protoTypeNumber" : "",
"status" : "A",
"classCode" : "OB",
"boatType" : "UT",
"powerType" : "OU",
"boatLength" : 18,
"horsePower" : 90,
"hullConstruction" : "A",
"msrp" : 10889,
"lowCostAmount" : 4900,
"highCostAmount" : 5970,
"retailAmount" : 6840,
"notes" : ""
}
]
}
]
}
我正在尝试过滤数据,以便可以带回mrsp值为9589的子模型。这是我目前的代码,但是它为子模型带来了一个空数组。
db.boats.aggregate({
$match: {
$and: [
{'make.name': 'Excel Boats'},
{'make.models.year': 2012},
{'make.models.name': '1860V86'}
]
}
},
{
$project: {
'make.models.subModels': {
$filter: {
input: '$make.models.subModels',
as: 'subModel',
cond: {$eq: ['$$subModel.msrp', 10889]}
}
}
}
}
).pretty()
这是我运行查询时的结果
{
"_id" : ObjectId("5c73cbeb2258414c5d9bf2a5"),
"make" : {
"models" : [
{
"subModels" : [ ]
}
]
}
}
我希望得到的是这个结果
{
"_id" : ObjectId("5c73cbeb2258414c5d9bf2a5"),
"make" : {
"models" : [
{
"subModels" : [ {
"name" : "",
"controlNumber" : "OB1133079",
"protoTypeNumber" : "",
"status" : "A",
"classCode" : "OB",
"boatType" : "UT",
"powerType" : "OU",
"boatLength" : 18,
"horsePower" : 70,
"hullConstruction" : "A",
"msrp" : 9589,
"lowCostAmount" : 4700,
"highCostAmount" : 5270,
"retailAmount" : 6540,
"notes" : ""
} ]
}
]
}
}
如果任何人都可以提供我要去哪里的任何信息,我将不胜感激。我感觉到它与数组中的嵌套数组有关,因为我能够毫无问题地获取模型数组中的信息。
谢谢!
答案 0 :(得分:0)
查询中的拼写错误。
R version 3.4.2 (2017-09-28)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.4 LTS
Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.0
LAPACK: /usr/lib/lapack/liblapack.so.3.0
other attached packages:
[1] h2o_3.22.1.1 forcats_0.3.0 stringr_1.3.1 dplyr_0.7.8 purrr_0.3.0 readr_1.2.1 tidyr_0.8.2 tibble_1.4.2 ggplot2_3.1.0 tidyverse_1.2.1