mongoose: populate match not working

What is wrong about my code? Tried to filter using match in populate but only getting null for b_id:

my code is as follows:

exports.search = function (req, res) {
    var b = req.body.b;
    var a = req.body.a;
    J.find()
        .populate({
            path: 'b_id',
            match: {name: b},
            model: B
        })
        .populate({
            path: 'w_id',
            model: W
        })
        .exec(function (err, j) {
            W.populate(j, {
                path: 'w_id.a_id',
                model: A
            }, function (err, j) {
                console.log(j);
            });
        });
}

everything works fine I leave out the match and vars a and b are also filled correctly.

Any suggestions?

Many thanks!

EDIT:

These are my schemas:

var B = new Schema({
    _id             : Number,
    name            : String
}, { collection: 'b' });

var A = new Schema({
    _id             : Number,
    name            : String
}, { collection: 'a' });

var W = new Schema({
    _id             : Number,
    a_id            : { type: Number, ref: 'a' },
    bez     : String
}, { collection: 'w' });

var J = new Schema({
    _id             : Number,
    b_id    : { type: Number, ref: 'b' },
    w_id        : { type: Number, ref: 'w' },
    s1          : String,
    e1          : String,
    s2          : String,
    e2          : String,
    bdh : String
}, { collection: 'j' });

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 15 (1 by maintainers)

Most upvoted comments

+1

Populate match does not filter. It just returns all the records.

var ruleSchema = new Schema({
    name: {type: String, unique: true},
    request: {type:Schema.Types.ObjectId, ref: 'Request'},
    response: {type:Schema.Types.ObjectId, ref: 'Response'},

    comment: "String",
    createTime: {type: Date, default: Date.now}
},{ strict: false });
var requestSchema = new Schema({
    //id: {type: String, unique: true},
    method: "String",
    scheme: "String",
    host: "String",
    port: "Number",
    //path: "path",
    firstPath: {type: String, trim: true },
    path: [String],// something like:  /product/abc/123
    queryString: "String", // stringfied parms
    fragment: "String", //Anchor

    headers: [String],

    comment: "String",
    createTime: {type: Date, default: Date.now}
},{ strict: false });
var queryRule = function(req, callback){
  Rule.find().populate({path:'request' , match:{method: 'POST'} }).exec(function(error, rules){
  //Rule.find().exec(function(error, rules){
    if(error){
      callback(error)
    }else{
      callback(null, rules)
    }
  });
}

I have inserted 4 records in Rule schema with only one record have method equals POST, but with the code, I can query out 4 records.

match does not work. When i test it with mocha it works fine but the same with angular fails.

If i remove match then it works in angular.

Program.find({‘isActive’: true}).populate({ path: ‘network’, match: { isVisible: true }, select: ‘_id name’ }).exec(function (err, programs) { res.json(programs); });