gpt4 book ai didi

嵌套数组上的mongodb聚合和排序

转载 作者:可可西里 更新时间:2023-11-01 10:46:03 25 4
gpt4 key购买 nike

下面是我的mongo db文档结构

{
"product_json": {
"productId": 1,
"productData": [
{
"productName": "A",
"productDetails": [
{
"manufacturedDate": "2014-08-09",
"name": "A1",
"size": 300
},
{
"manufacturedDate": "2012-08-09",
"name": "A2",
"size": 200
}
]
},
{
"productName": "B",
"productDetails": [
{
"manufacturedDate": "2015-08-09",
"name": "B1",
"size": 300
},
{
"manufacturedDate": "2017-08-09",
"name": "B2",
"size": 200
}
]
}
]
}
}

我需要根据“制造日期”进行分组并应用排序。此外,我不希望整个文档都匹配。只有匹配的对象(匹配的 productDetails 对象)。

最佳答案

试试这个查询:

db.collection.aggregate(
[
{$sort: {'product_json.productData.productDetails.manufacturedDate': 1}},
{ $group:{
"_id": "$product_json.productData.productDetails.manufacturedDate"
}
}
]
);

关于嵌套数组上的mongodb聚合和排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52182029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com