Empty sub-docs when no fields selected
I have a Schema defined as the following:
var CarSchema = new Schema({
carplate: { type:String, index: true, required: true},
features: { optA: String, optB: String},
notes : String
},{strict: "throw"});
and I reference this in another Schema, like in (Car is the model derived by the previous Schema):
var ReservationSchema = new Schema({
car: {type: Schema.Types.ObjectId, ref: 'Car'},
...
notes : String,
...
}, {strict: "throw"});
Well, when I run a query with a populate like the following
Reservation.findOne({}).populate('car', '_id carplate').exec(function(err, results){...});
Population works but I'm always obtaining also the field named 'features', despite it's not selected in populate(). From my further tests I'm observing it happens with all "complex" fields in Schemas.
Is it a feature? Thank you.
Thanks for pointing this out, I'll investigate when I get a chance.
I'm also seeing this, returns selected field plus all other object type fields.
Hmm I'm going to need some help reproducing this issue @ikb42 . The below code:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var db = mongoose.createConnection('mongodb://localhost/tests');
var CarSchema = new Schema({
carplate: { type:String, index: true, required: true},
features: { optA: String, optB: String},
notes : String
},{strict: "throw"});
var ReservationSchema = new Schema({
car: {type: Schema.Types.ObjectId, ref: 'Car'},
}, {strict: "throw"});
var Car = db.model('Car', CarSchema);
var Reservation = db.model('Reservation', ReservationSchema);
Car.remove({}, function() {
Reservation.remove({}, function() {
Car.create({ carplate: 'BACONATOR', features: { optA: 'A', optB: 'B' } }, function(err, car) {
console.log('err: ' + err);
Reservation.create({ car: car._id }, function(err, reservation) {
console.log('err2: ' + err);
Reservation.findOne({}).populate('car', '_id carplate').exec(function(err, reservation) {
console.log('err3: ' + err);
console.log(JSON.stringify(reservation));
console.log(reservation.features);
});
});
});
});
});
Provides the expected output (no features in resulting doc) in 3.6.17, 3.8.22, and 4.0.0-rc1.
Hi Sorry for the delay.
For example - see the addition of "thisstuff" field below (no other changes), even though this has no data it will be returned by the findOne.
var ReservationSchema = new Schema({
car: {type: Schema.Types.ObjectId, ref: 'Car'},
thisstuff: [{
astring: String
}]
}, {strict: "throw"});
Ah ok I see what you mean. The below code:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var db = mongoose.createConnection('mongodb://localhost/tests');
var CarSchema = new Schema({
carplate: { type:String, index: true, required: true},
features: { optA: String, optB: String},
notes : String
},{strict: "throw"});
var ReservationSchema = new Schema({
car: {type: Schema.Types.ObjectId, ref: 'Car'},
thisstuff: [{
astring: String
}]
}, {strict: "throw"});
var Car = db.model('Car', CarSchema);
var Reservation = db.model('Reservation', ReservationSchema);
Car.remove({}, function() {
Reservation.remove({}, function() {
Car.create({ carplate: 'BACONATOR', features: { optA: 'A', optB: 'B' } }, function(err, car) {
Reservation.create({ car: car._id }, function(err, reservation) {
Reservation.findOne({}).populate('car', '_id carplate').exec(function(err, reservation) {
console.log(JSON.stringify(reservation));
console.log(reservation.car.features);
});
});
});
});
});
Gives me the bad output. reservation.car.features outputs an empty object. Thanks for pointing this out.
Thank you for a really great tool
Unfortunately I'm going to have to kick the can down the road on this one. This issue is due to limitations in mpath - mpath doesn't do proper safe navigation for set(), and I don't have push access for mpath. I have done the "fork and publish new npm module" approach before, but that was because hooks had a critical bug that couldn't wait. IMO this is a relatively minor limitation, and goes away on the client-side with the minimize option.
No problem, I've worked around for now. I'll take a look at the minimize option in the mean time too, missed that before.
Looks like this was fixed somewhere between 3.8 and 5.x:
$ node gh-2503.js
(node:4522) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(node:4522) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
(node:4522) DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.
{"_id":"5beae4de99d40b11aaca1216","car":{"_id":"5beae4de99d40b11aaca1215","carplate":"BACONATOR"},"__v":0}
undefined
^C
$
Slightly modified script:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var db = mongoose.createConnection('mongodb://localhost/tests');
var CarSchema = new Schema({
carplate: { type:String, index: true, required: true},
features: { optA: String, optB: String},
notes : String
},{strict: "throw"});
var ReservationSchema = new Schema({
car: {type: Schema.Types.ObjectId, ref: 'Car'},
thisstuff: { type : [{
astring: String
}], default: void 0 }
}, {strict: "throw"});
var Car = db.model('Car', CarSchema);
var Reservation = db.model('Reservation', ReservationSchema);
Car.remove({}, function() {
Reservation.remove({}, function() {
Car.create({ carplate: 'BACONATOR', features: { optA: 'A', optB: 'B' } }, function(err, car) {
Reservation.create({ car: car._id }, function(err, reservation) {
Reservation.findOne({}).populate('car', '_id carplate').exec(function(err, reservation) {
console.log(JSON.stringify(reservation));
console.log(reservation.car.features);
});
});
});
});
});
Will close for now since it looks like it works in 5.3.9. Re-open if this is still an issue.