find返回所需的数据,但mongodb中没有$ match

时间:2019-01-23 21:02:12

标签: node.js mongodb nosql mongodb-query nosql-aggregation

我正在尝试使用$ match获取所需的数据,但每次都返回null。另一方面,find返回所需的数据。我必须在获得的结果上实现$ lookup和$ merge,但$ match结果为空。

我尝试在$ match中使用基本查询,但仍返回null。我也尝试过其他帖子,但是在每个帖子中给出的步骤都不令我满意。

这是我的代码:

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 个答案:

没有答案