Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB-将结果聚合并连接到组_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

MongoDB-将结果聚合并连接到组

MongoDB-将结果聚合并连接到组,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我有一些项目收藏如下: [ { name: 'item1', description: 'description #1', categories: 'cat_A; cat_B'}, { name: 'item2', description: 'description #2', categories: 'cat_B'}, { name: 'item3', description: 'description #3', categories: 'cat_C; cat_B'},

我有一些
项目
收藏如下:

[
    { name: 'item1', description: 'description #1', categories: 'cat_A; cat_B'},
    { name: 'item2', description: 'description #2', categories: 'cat_B'},
    { name: 'item3', description: 'description #3', categories: 'cat_C; cat_B'},
    { name: 'item4', description: 'description #4', categories: 'cat_B; cat_A'},
    { name: 'item5', description: 'description #5', categories: 'cat_B'},
    { name: 'item6', description: 'description #6', categories: 'cat_D'}
]
我想按类别查找和筛选结果。我创建了mongo查询:

db.getCollection('items')
    .aggregate([
        {
            $match: {
                categories: {$in: [/cat_a/i, /cat_b/i]}
            }
        }, {
            $group: {
                _id: "$categories",
                items: { $push:  { name: "$name", description: '$description' } }
            }
        }
    ])
所以它返回给我这个:

result : [
    {
        "_id" : "cat_B; cat_C",
        "items" : [
            {
                "name" : "item3",
                "description" : "description #3"
            }
        ]
    }, {
        "_id" : "cat_B; cat_A",
        "items" : [
            {
                "name" : "item4",
                "description" : "description #4"
            }
        ]
    }, {
        "_id" : "cat_B",
        "items" : [
            {
                "name" : "item2",
                "description" : "description #2"
            },
            {
                "name" : "item5",
                "description" : "description #5"
            }
        ]
    }, {
        "_id" : "cat_A; cat_B",
        "items" : [
            {
                "name" : "item1",
                "description" : "description #1"
            }
        ]
    }
]
我想要实现的是:

result : [
    {
        "_id" : "cat_A",
        "items" : [
            {
                "name" : "item1",
                "description" : "description #1"
            },
            {
                "name" : "item4",
                "description" : "description #4"
            }
        ]
    }, {
        "_id" : "cat_B",
        "items" : [
            {
                "name" : "item1",
                "description" : "description #1"
            },
            {
                "name" : "item2",
                "description" : "description #2"
            },
            {
                "name" : "item3",
                "description" : "description #3"
            },
            {
                "name" : "item4",
                "description" : "description #4"
            },
            {
                "name" : "item5",
                "description" : "description #5"
            }
        ]
    }
]

在纯mongo查询中可能吗?

使用聚合框架,您需要一种机制将
类别
字符串拆分为一个不同的集合,但这样的操作符还不存在;最接近的是运算符,它需要知道索引位置的索引和要提取的子字符串的指定字符数,这几乎是不可能的。因此,建议将类别存储为不同类别名称的数组

--编辑--

如果您想保持
类别
字段不变,那么我建议您创建一个额外的字段来存储类别列表,然后您可以在该字段上运行聚合管道以获得所需的结果

让我们用一个例子来演示上述方法:

更改架构

db.items.aggregate([
    { "$match": { "categoriesList": { "$in": ['cat_A', 'cat_B'] } } },
    { "$unwind": "$categoriesList" },
    {
        "$group": {
            "_id": "$categoriesList",
            "items": { "$push":  { "name": "$name", "description": '$description' } }
        }
    }
])
a) 如果使用MongoDB v3.0或更低版本:

b) 如果使用MongoDB v3.2.X或更高版本:

在新架构上运行聚合

db.items.aggregate([
    { "$match": { "categoriesList": { "$in": ['cat_A', 'cat_B'] } } },
    { "$unwind": "$categoriesList" },
    {
        "$group": {
            "_id": "$categoriesList",
            "items": { "$push":  { "name": "$name", "description": '$description' } }
        }
    }
])

我们可以使用
split(;”)
trim
进行小型清理。不幸的是,我无法修改此集合中的任何字段,我需要将
categories
保留为字符串。然后创建一个额外字段来存储不同的类别集,您可以保留原始的categories字段。当您运行聚合管道时,您可以
$unwind
并按新字段分组。我也不能这样做。在我的例子中,items集合和DB是只读的。我想我会留下简单的mongo
。在后端得到响应后,找到方法并按类别分组数据。谢谢你的帮助。
db.items.aggregate([
    { "$match": { "categoriesList": { "$in": ['cat_A', 'cat_B'] } } },
    { "$unwind": "$categoriesList" },
    {
        "$group": {
            "_id": "$categoriesList",
            "items": { "$push":  { "name": "$name", "description": '$description' } }
        }
    }
])