MongoDb $addFields 和 $match

sop*_*ith 5 aggregation mongodb node.js

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

聚合顺序

什么是聚合

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
                }
            },
        ]);
Run Code Online (Sandbox Code Playgroud)

Ale*_* P. 6

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

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

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

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"
        }
    }
])
Run Code Online (Sandbox Code Playgroud)

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

{"projectName": {$regex: '.*' + req.query.search + '.*', $options: "i"}},       
{'document.id': {$regex: '.*' + req.query.search + '.*', $options: "i"}}
Run Code Online (Sandbox Code Playgroud)