我有这个问题,但我不能解决。我必须转换数组 s
的新字段,称为 shares
. 这个新字段里面有新的键和新的值。假设我有这些文件。
{
'name': 'igor',
's': ['a', 'a', 'a', 'b', 'b']
},
{
'name': 'jones',
's': ['c', 'b']
}
预期输出:
{
'name': 'igor',
'shares': {
'a': 3
'b': 2
}
},
{
'name': 'jones',
'shares': {
'c': 1
'b': 1
}
}
你可以试试下面的聚合查询。
db.collection.aggregate([
/** unwind `s` array */
{
$unwind: "$s"
},
/** group on unique pairs of `_id + s` & retain name field, count sum of matching docs */
{
$group: { _id: { k: "$s", _id: "$_id" }, name: { $first: "$name" }, v: { $sum: 1 } }
},
/** group on unique pairs of just `_id` & retain name field, push docs into shares array `[{k :..., v:...}]` */
{
$group: { _id: "$_id._id", name: { $first: "$name" }, shares: { $push: { k: "$_id.k", v: "$v" } } }
},
/** Re-create shares field from array to object */
{
$addFields: { shares: { $arrayToObject: "$shares" } }
}
])
测试: mongoplayground
在一个数组中添加异构元素(在你的例子中。'a': 3, 'b': 2
)到一个数组中,我将 shares
'的类型改为类似。
{
key: "$_id.shares",
count: "$count"
}
你需要按顺序执行以下操作
s
.name
和 s
._id.name
和推送类型的对象 key
和 count
至 shares
数组。你可以尝试下面的查询。
db.collection.aggregate([
{
$unwind: "$s"
},
{
$group: {
_id: {
name: "$name",
shares: "$s"
},
count: {
$sum: 1
}
}
},
{
$group: {
_id: "$_id.name",
shares: {
$push: {
key: "$_id.shares",
count: "$count"
}
}
}
}
])
输出
[
{
"_id": "jones",
"shares": [
{
"count": 1,
"key": "c"
},
{
"count": 1,
"key": "b"
}
]
},
{
"_id": "igor",
"shares": [
{
"count": 3,
"key": "a"
},
{
"count": 2,
"key": "b"
}
]
}
]