Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDb $addFields and $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.

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
                }
            },
        ]);
like image 517
sophanith Avatar asked Nov 15 '17 14:11

sophanith


People also ask

What is $addFields in MongoDB?

Definition. Adds new fields to documents. $addFields outputs documents that contain all existing fields from the input documents and newly added fields. The $addFields stage is equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields.

What does $match do in MongoDB?

The MongoDB $match operator filters the documents to pass only those documents that match the specified condition(s) to the next pipeline stage.

What is the use of match and group operator in MongoDB?

The $match stage filters the documents to only pass documents from the year 2014 to the next stage. Second Stage: The $group stage groups the documents by date and calculates the total sale amount, average quantity, and total count of the documents in each group.

What can the $match aggregation stage be used for?

The $match stage of the pipeline can be used to filter documents so that only ones meeting certain criteria move on to the next stage. In this article, we'll discuss the $match stage in more detail and provide examples that illustrate how to perform match aggregation in MongoDB.


1 Answers

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"
        }
    }
])

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"}}
like image 163
Alex P. Avatar answered Oct 28 '22 09:10

Alex P.