Note that there are some explanatory texts on larger screens.

plurals
  1. POpopulate based on condition in mongoose js not working
    primarykey
    data
    text
    <p>I have 2 schema</p> <pre><code>var Schema = require('mongoose').Schema; var DeviceSchema = new Schema({ name: { type: String }, code: { type: String, unique: true }, os: { type: String }, userEmail: { type: String }, checkoutTime: { type: Date }, tags: { type: [String] } }); module.exports = DeviceSchema; var Schema = require('mongoose').Schema; var TrackHistorySchema = new Schema({ userEmail: { type: String }, device: { type: Schema.ObjectId, ref: 'Device', required: true }, checkinTime: { type: Date }, checkoutTime: { type: Date, 'default': Date.now } }); module.exports = TrackHistorySchema; </code></pre> <p>I have a search track history with criteria user email id and device name. </p> <p>This works if I used on email criteria from history table </p> <pre><code>TrackHistory.find({'userEmail': req.query.searchText}) .populate({path: 'device'}) .sort('-name') .skip(page * maxHistoryPerPage) .limit(maxHistoryPerPage) .exec(next); </code></pre> <p>But it will not work if I used device name criteria from device table</p> <pre><code>TrackHistory.find({}) .populate({path: 'device', match: {name: req.query.searchText}}) .sort('-name') .skip(page * maxHistoryPerPage) .limit(maxHistoryPerPage) .exec(next); </code></pre> <p>I am getting all rows of history with device columns null, just like a left join in mysql. I wanted an inner join like functionality, that is if device name not matching then the corresponding history rows also should not be shown. I tried different combinations from mongoose documentation. Tried where clause also.</p> <p>I am using mongoose 3.6.2 (latest available as of now)</p> <h1>Edit - adding model code</h1> <pre><code>var mongoose = require('mongoose'); var DeviceSchema = require('../schemas/device'); var Device = mongoose.model('Device', DeviceSchema); module.exports = Device; var mongoose = require('mongoose'); var TrackHistorySchema = require('../schemas/track_history'); var TrackHistory = mongoose.model('TrackHistory', TrackHistorySchema); module.exports = TrackHistory; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload