Laravel 计算MongoDB中与子数组条件匹配的文档
我有一个包含许多文档的数据库,其结构类似于:Laravel 计算MongoDB中与子数组条件匹配的文档,laravel,mongodb,aggregation-framework,Laravel,Mongodb,Aggregation Framework,我有一个包含许多文档的数据库,其结构类似于: { "_id" : ObjectId("586c0f07d0ad7a2b8b08c572"), "auto_vehicles" : [ { "year" : "2017", "make" : "SUBARU", }, { "year" : "2011", "make" : "CHEVROLET",
{
"_id" : ObjectId("586c0f07d0ad7a2b8b08c572"),
"auto_vehicles" : [
{
"year" : "2017",
"make" : "SUBARU",
},
{
"year" : "2011",
"make" : "CHEVROLET",
}
],
"created_at" : ISODate("2017-01-03T20:52:23.192Z")
}
假设我想统计所有具有“auto_vehicles.make”等于“SUBARU”的文档,我尝试了以下操作:
collection.find({'auto_vehicles.make':'SUBARU'}).count()
但是返回的值似乎不正确(基本上没有表示实际的数据库数量)
我怀疑find可能不会像这样在子数组中搜索
其他信息:我通过Laravel 5.2 w/Jenssegers MongoDB雄辩模型和来自
非常感谢您的想法-谢谢您您可以试试这样的东西。由于需要所有文档中的计数,因此必须使用聚合 $match仅保留具有匹配make的嵌入式阵列
db.collection.aggregate([{
$match: {
"auto_vehicles.make": 'SUBARU'
}
}, {
$unwind: "$auto_vehicles"
},
{
$match: {
"auto_vehicles.make": 'SUBARU'
}
},{
$group: {
_id: "$auto_vehicles.make",
count: {
$sum: 1
}
}
}]);
$展开嵌入式阵列
$match仅保留匹配的品牌
db.collection.aggregate([{
$match: {
"auto_vehicles.make": 'SUBARU'
}
}, {
$unwind: "$auto_vehicles"
},
{
$match: {
"auto_vehicles.make": 'SUBARU'
}
},{
$group: {
_id: "$auto_vehicles.make",
count: {
$sum: 1
}
}
}]);
$group对匹配的品牌求和
db.collection.aggregate([{
$match: {
"auto_vehicles.make": 'SUBARU'
}
}, {
$unwind: "$auto_vehicles"
},
{
$match: {
"auto_vehicles.make": 'SUBARU'
}
},{
$group: {
_id: "$auto_vehicles.make",
count: {
$sum: 1
}
}
}]);
更新:
根据匹配条件对嵌入文档进行$filter
db.collection.aggregate([{
"$project": {
"_id": 1,
"auto_vehicles": {
"$filter": {
"input": "$auto_vehicles",
"as": "result",
"cond": {
"$eq": ["$$result.make", 'SUBARU']
}
}
}
}
}, {
$unwind: "$auto_vehicles"
}, {
$group: {
_id: "$auto_vehicles.make",
count: {
$sum: 1
}
}
}]);
你可以试试这样的。由于需要所有文档中的计数,因此必须使用聚合 $match仅保留具有匹配make的嵌入式阵列
db.collection.aggregate([{
$match: {
"auto_vehicles.make": 'SUBARU'
}
}, {
$unwind: "$auto_vehicles"
},
{
$match: {
"auto_vehicles.make": 'SUBARU'
}
},{
$group: {
_id: "$auto_vehicles.make",
count: {
$sum: 1
}
}
}]);
$展开嵌入式阵列
$match仅保留匹配的品牌
db.collection.aggregate([{
$match: {
"auto_vehicles.make": 'SUBARU'
}
}, {
$unwind: "$auto_vehicles"
},
{
$match: {
"auto_vehicles.make": 'SUBARU'
}
},{
$group: {
_id: "$auto_vehicles.make",
count: {
$sum: 1
}
}
}]);
$group对匹配的品牌求和
db.collection.aggregate([{
$match: {
"auto_vehicles.make": 'SUBARU'
}
}, {
$unwind: "$auto_vehicles"
},
{
$match: {
"auto_vehicles.make": 'SUBARU'
}
},{
$group: {
_id: "$auto_vehicles.make",
count: {
$sum: 1
}
}
}]);
更新:
根据匹配条件对嵌入文档进行$filter
db.collection.aggregate([{
"$project": {
"_id": 1,
"auto_vehicles": {
"$filter": {
"input": "$auto_vehicles",
"as": "result",
"cond": {
"$eq": ["$$result.make", 'SUBARU']
}
}
}
}
}, {
$unwind: "$auto_vehicles"
}, {
$group: {
_id: "$auto_vehicles.make",
count: {
$sum: 1
}
}
}]);
Thankyu@RyanWheale的可能副本-我不知道该项目如何关联-也许我遗漏了重叠部分/您可以进一步解释?谢谢你啊,我一读就误读了你的问题。无论哪种方式,在查询这样的子文档时都需要使用聚合。结帐
$match
,$unwind
,和$group
Thank you@RyanWheale的可能重复项-我看不出该项有什么关系-也许我缺少重叠项/您可以进一步解释?谢谢你啊,我一读就误读了你的问题。无论哪种方式,在查询这样的子文档时都需要使用聚合。签出$match
,$unwind
和$group
虽然我认为第二个$match是多余的,但这是有效的-我需要运行一个实验来确定。非常感谢您的时间和努力!虽然我相信第二个$match是多余的,但这是可行的——不过我需要做一个实验来确定。非常感谢您的时间和努力!