node.js - MongoDb $addFields 和 $match

标签 node.js mongodb aggregation

在我的 mongodb 查询中,我使用 $addFields 添加 ID 字段,该字段连接了其他三个字段。我的问题是,如果我将新添加的字段与我要查询的值匹配,我将得不到任何结果。对于其他领域,它们工作得很好。

order of aggregation

what is in aggregation

data = await model.aggregate([
            {
                $project: {
                    projectName: 1,
                    price: 1,
                    'document': '$$ROOT'
                }
            },
            {
                $addFields:{
                    'document.id': {$concat: ['$document.propertyId.prefix','$document.propertyId.number']}
                }
            },
            {
                $match: {
                    $and: [
                        {
                            $or: [
                                {id: {$regex: '.*' + req.query.search + '.*', $options: "i"}},
                                {projectName: {$regex: '.*' + req.query.search + '.*', $options: "i"}},

                                /*This also doesnt work*/
                                // {'document.id': {$regex: '.*' + req.query.search + '.*', $options: "i"}},
                                // {'document.projectName': {$regex: '.*' + req.query.search + '.*', $options: "i"}},
                            ]
                        }
                    ]
                }
            },
            {
                $replaceRoot: {newRoot: "$document"}
            },
            {
                $sort: {
                    [sortBy]: sortType
                }
            },
        ]);

最佳答案

下次请添加您的文档示例,以便人们可以重现您的问题。话虽如此,我看不出你的问题在哪里。我创建了一些数据来重现您的用例。所以我添加了以下文件:

{ 
    "_id" : ObjectId("5a0d5d376c9b762a7c035ec4"), 
    "projectName" : "some stack test", 
    "price" : NumberInt(45), 
    "propertyId" : {
        "prefix" : "a", 
        "number" : "7"
    }
}

然后我执行了你的脚本(没有排序)并且它工作正常:

db.yourCollectionName.aggregate([
    {
        $project: {
            "projectName": 1,
            "price": 1,
            "document": '$$ROOT'
        }
    },
    {
        $addFields: {
            "document.id": {
                $concat: ['$document.propertyId.prefix', '$document.propertyId.number']
            }
        }
    },
    {
        $match: {
            $and: [{
                $or: [{
                        "projectName": {
                            $regex: '.*' + "some stack test"
                        }
                    },

                    {
                        "document.id": {
                            $regex: '.*' + "a" + '.*',
                            $options: "7"
                        }
                    }
                ]
            }]

        }
    },
    {
        $replaceRoot: {
            newRoot: "$document"
        }
    }
])

您没有得到任何结果的事实可能是由于您的请求参数。除了 "projectName" 与您的 "document.id" 具有相同的搜索参数之外,它们还匹配吗? 再次检查您的匹配管道:

{"projectName": {$regex: '.*' + req.query.search + '.*', $options: "i"}},       
{'document.id': {$regex: '.*' + req.query.search + '.*', $options: "i"}}

关于node.js - MongoDb $addFields 和 $match,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47309861/

相关文章:

javascript - Appfog,nodejs,环境变量: service doesn't contain at one point but does at another

mongodb - 在 Azure DevOps Pipeline CI 中使用 MongoDB

MongoDB 集合

java - Kafka 流记录在窗口/聚合后不转发

node.js - mlab/MongoDB atlas 和自托管 mongodb 有什么区别

node.js - 为什么 npm list -g 会导致 npm ERR!遗漏错误?

node.js - Nodejs 和 PassportJs : Redirect middleware after passport. 如果身份验证失败,则不会调用身份验证

javascript - 如何检查用户是否存在?

python - 在 Mongoengine 中聚合引用字段

types - 按类型限制 ElasticSearch 聚合?