MongoDb $ addFields和$ match [英] MongoDb $addFields and $match

查看:558
本文介绍了MongoDb $ addFields和$ match的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my mongodb query, I use $addFields to add ID field which concatenated of other three fields. My problem is that I get no result if I $match that new added field with value that I want to query. For other fields, they work just fine.

聚合顺序

聚合的内容

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
                }
            },
        ]);


推荐答案

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

Next time please add a sample of your document so people can reproduce your problem. Having said that I don't see where your problem is. I created some data to reproduce your usecase. So I added the following document:

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

然后我执行了您的脚本(无需排序),并且可以正常工作:

Then I executed your script (without the sort) and it works fine:

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 相同的搜索参数外,它们甚至匹配吗?
再次检查您的比赛管道:

The fact that you get no results is probably due to your request parameters. Besides how can the "projectName"have the same search parameters as your "document.id" Do they even match? Check your match pipeline again:

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

这篇关于MongoDb $ addFields和$ match的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆