在2个不同的字段中搜索值mongodb + node.js

时间:2017-02-08 13:20:38

标签: node.js mongodb mongodb-query aggregation-framework

我是新手。但我尝试学习编写查询的最合理方法。

假设我收藏的是;

{ 
    "id" : NumberInt(1), 
    "school" : [
        {
            "name" : "george", 
            "code" : "01"
        }, 
        {
            "name" : "michelangelo", 
            "code" : "01"
        }
    ], 
    "enrolledStudents" : [
        {
            "userName" : "elisabeth", 
            "code" : NumberInt(21)
        }
    ]
}
{ 
    "id" : NumberInt(2), 
    "school" : [
        {
            "name" : "leonarda da vinci", 
            "code" : "01"
        }
    ], 
    "enrolledStudents" : [
        {
            "userName" : "michelangelo", 
            "code" : NumberInt(25)
        }
    ]
}

我想列出key的相应code值的出现次数。

作为示例keymichelangelo

为了找到密钥的出现,我写了两个不同的aggregation查询;

db.test.aggregate([
    {$unwind: "$school"},
    {$match : {"school.name" : "michelangelo"}},
    {$project: {_id: "$id", "key" : "$school.name", "code" : "$school.code"}}
])

db.test.aggregate([
    {$unwind: "$enrolledStudents"},
    {$match : {"enrolledStudents.userName" : "michelangelo"}},
    {$project: {_id: "$id", "key" : "$enrolledStudents.userName", "code" : "$enrolledStudents.code"}}
])

这两个查询的结果返回了我想要的内容;

{ "_id" : 1, "key" : "michelangelo", "code" : "01" }
{ "_id" : 2, "key" : "michelangelo", "code" : 25 }

其中一个要在enrolledStudents中搜索,另一个是在school字段中搜索。

这两个查询可以简化为更符合逻辑的查询吗?或者这是唯一的方法吗?

ps:我知道数据库结构不符合逻辑,但我试图模拟。

修改 我尝试用find编写查询。

db.test.find({$or: [{"enrolledStudents.userName" : "michelangelo"} , {"school.name" : "michelangelo"}]}).pretty()

但这会将整个文档返回为;

{
    "id" : 1,
    "school" : [
        {
            "name" : "george",
            "code" : "01"
        },
        {
            "name" : "michelangelo",
            "code" : "01"
        }
    ],
    "enrolledStudents" : [
        {
            "userName" : "elisabeth",
            "code" : 21
        }
    ]
}
{
    "id" : 2,
    "school" : [
        {
            "name" : "leonarda da vinci",
            "code" : "01"
        }
    ],
    "enrolledStudents" : [
        {
            "userName" : "michelangelo",
            "code" : 25
        }
    ]
}

1 个答案:

答案 0 :(得分:2)

Mongo 3.4

$match - 此阶段将保留所有school数组和enrolledStudents,其中至少有一个嵌入文档与查询条件匹配

$group - 此阶段会将所有schoolenrolledStudents数组合并为组中每个_id的2d数组。

$project - 此阶段$filter merge数组用于匹配查询条件,$map数组用新标签values数组。

$unwind - 此阶段将使阵列变平。

$addFields& $replaceRoot - 此阶段将添加id字段并将values数组提升到顶部。

db.collection.aggregate([
    {$match : {$or: [{"enrolledStudents.userName" : "michelangelo"} , {"school.name" : "michelangelo"}]}},
    {$group: {_id: "$id", merge : {$push:{$setUnion:["$school", "$enrolledStudents"]}}}},
    {$project: {
        values: {
              $map:
                 {
                   input: {
                            $filter: {
                                input: {"$arrayElemAt":["$merge",0]},
                                as: "onef",
                                cond: {
                                    $or: [{
                                        $eq: ["$$onef.userName", "michelangelo"]
                                    }, {
                                        $eq: ["$$onef.name", "michelangelo"]
                                    }]
                                }
                            }
                        },
                   as: "onem",
                   in: { 
                         key : { $ifNull: [ "$$onem.userName", "$$onem.name" ] },
                         code : "$$onem.code"}
                 }
            }
        }
    },
    {$unwind: "$values"},
    {$addFields:{"values.id":"$_id"}},
    {$replaceRoot: { newRoot:"$values"}}
])

样本回复

{ "_id" : 2, "key" : "michelangelo", "code" : 25 }
{ "_id" : 1, "key" : "michelangelo", "code" : "01" }

Mongo< = 3.2

将上述聚合的最后两个阶段替换为$project以格式化响应。

{$project: {"_id": 0 , id:"$_id", key:"$values.key", code:"$values.code"}}

样本回复

{ "_id" : 2, "key" : "michelangelo", "code" : 25 }
{ "_id" : 1, "key" : "michelangelo", "code" : "01" }

您可以使用$redact代替$group& match并添加$project $map以格式化回复。

$redact一次浏览一个文档级别,并根据匹配条件执行$$DESCEND$$PRUNE

唯一需要注意的是在$ifNull的第一个文档级别使用id,以便您$$DESCEND可以嵌入文档级别以进行进一步处理。

db.collection.aggregate([
    {
        $redact: {
            $cond: [{
                $or: [{
                    $eq: ["$userName", "michelangelo"]
                }, {
                    $eq: ["$name", "michelangelo"]
                }, {
                    $ifNull: ["$id", false]
                }]
            }, "$$DESCEND", "$$PRUNE"]
        }
    },
    {
        $project: {
            id:1,
            values: {
              $map:
                 {
                   input: {$setUnion:["$school", "$enrolledStudents"]},
                   as: "onem",
                   in: { 
                         key : { $ifNull: [ "$$onem.userName", "$$onem.name" ] },
                         code : "$$onem.code"}
                 }
            }
        }
    },
    {$unwind: "$values"},
    {$project: {_id:0,id:"$id", key:"$values.key", code:"$values.code"}}
])