gpt4 book ai didi

mongodb - 按字段分组文档并合并字段

转载 作者:可可西里 更新时间:2023-11-01 09:52:08 24 4
gpt4 key购买 nike

我有这样的文档结构

{
"_id" : ObjectId("63b6757dfa46345e268b7ba5"),
"name" : "Blue Skinny Jeans",
"sizes" : [45,46],
"textile" : "Wool",
"merchantCategory" : "Jeans",
"tags" : ["Tag 1","Tag 2"],
"sku" : "12161-2753930-5053177208071",
"manufacturer" : "Hugo Boss"
}

/* 1 */
{
"_id" : ObjectId("53b6757dfa46345e268b7ba4"),
"name" : "Blue Skinny Jeans",
"sizes" : [40,41],
"textile" : "Polyester",
"merchantCategory" : "Jeans",
"tags" : ["Tag 2","Tag 3"],
"sku" : "12161-2753930-5053177208071",
"manufacturer" : "Hugo Boss"
}

是否有可能构建一个查询按名称“分组”文档合并字段不相等(尺寸、标签、商品类别、纺织品)

最佳答案

这可能有用,

coll.aggregate({"$group":{"_id":"$name",
"sizes":{"$addToSet":"$sizes"},
"tags":{"$addToSet":"$tags"},
..
..
})

关于mongodb - 按字段分组文档并合并字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24573616/

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