0

I am trying to get required data using $match but it is returning null every time. On the other hand find returns the required data. I have to implement $lookup and $merge on the result I get but $match results null.

I have tried using a basic query in $match but it still returns null. I tried other posts too but in every post the steps that are given are not to my satisfaction.

This is my code:

var posts;
var area = "addresses."
var range = area.concat(req.body.range);
var place = req.body.place;
var reg = new RegExp(place, "i");
var query = {};
query[range] = reg;
/*Posts.find(query)*/ // this returns the result
Posts.aggregate( // inside this I get posts not found and 
                // the console.log prints empty value
    [
        {$match: {query}}
    ],
    function(err, posts) {
        if (err) {
            return res.send(err).end;
        } else if(posts.length > 0){
            console.log("posts = "+posts);
            reply[Constant.REPLY.MESSAGE] = "post by user found";
            reply[Constant.REPLY.DATA] = posts;
            reply[Constant.REPLY.RESULT_CODE] = 200;
            reply[Constant.REPLY.TOKEN] = "";
            return res.send(reply).end;
        }else {
            console.log("posts = "+posts); //empty value
            reply[Constant.REPLY.MESSAGE] = "posts not found";
            reply[Constant.REPLY.DATA] = null;
            reply[Constant.REPLY.RESULT_CODE] = 200;
            reply[Constant.REPLY.TOKEN] = "";
            return res.send(reply).end;
        }
    }
);

The result of both find and $match must be same.

0 Answers0