question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

populate match not working

See original GitHub issue

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' });

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

11reactions
ChristianRichcommented, Nov 6, 2015

+1

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

5reactions
YoleYucommented, Jan 8, 2015
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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose - Populate not filtering correctly - Stack Overflow
Match works when fields are part of the schema of the referred object. An option is to remove the status and balance fields...
Read more >
Mongoose v6.8.2: Query Population
Populate Virtuals: The Match Option ... You can also set the match option to a function. That allows configuring the match based on...
Read more >
mongoose populate function is not working. Can anyone tell me why ...
Guy's why this query is not working to fetch match data from two table order and user. although everything seem fine... Query code:...
Read more >
Populate doesnt work on mongoose
I've been trying to populate Auth with Voters Document but it doesnt seem to work. Sorry for the long a$$ code const voterSchema...
Read more >
Mongoose: Problem populating nested array with aggregate
There are two problems with this query. PROBLEM 1: It only populates institution because the institution $lookup stage was added after the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found