I have 2 schema
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;
I have a search track history with criteria user email id and device name.
This works if I used on email criteria from history table
TrackHistory.find({'userEmail': req.query.searchText})
.populate({path: 'device'})
.sort('-name')
.skip(page * maxHistoryPerPage)
.limit(maxHistoryPerPage)
.exec(next);
But it will not work if I used device name criteria from device table
TrackHistory.find({})
.populate({path: 'device', match: {name: req.query.searchText}})
.sort('-name')
.skip(page * maxHistoryPerPage)
.limit(maxHistoryPerPage)
.exec(next);
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.
I am using mongoose 3.6.2 (latest available as of now)
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;