Document Missing Property when not using lean()
Do you want to request a feature or report a bug? bug
What is the current behavior? I have a simple schema
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var FAQSchema = new Schema(
{
question: String,
title: String,
answer: String,
importance: { type: Number, default: 500 },
tags: { type: [String], index: true }
},
{
versionKey: false,
id: false,
toJSON: {
virtuals: true
},
toObject: {
virtuals: true
}
}
);
mongoose.model("FAQ", FAQSchema);
This query used to contain all the properties but now only contains the properties question, title, answer, importance and _id but NOT tags.
faqs = await FAQ.find({})
.exec();
res.json(faqs);
This query contains all the properties
faqs = await FAQ.find({})
.lean()
.exec();
res.json(faqs);
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
faqs = await FAQ.find({})
.exec();
res.json(faqs);
should contain all properties; this had been working for over a year, haven't changed the schema
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Mongoose 5.9.2, also happening on 5.8.2 mongodb 4.0.9 & 4.2.3 node 12.14.1 & 12.15.0
@sbubaron the following code works as expected. Can you edit it to reproduce your issue?
8615.js
#!/usr/bin/env node
'use strict';
const assert = require('assert');
const mongoose = require('mongoose');
const { Schema, connection } = mongoose;
const DB = '8615';
const URI = `mongodb://localhost:27017/${DB}`;
const OPTS = {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
};
const FAQSchema = new Schema(
{
question: String,
title: String,
answer: String,
importance: { type: Number, default: 500 },
tags: { type: [String], index: true }
},
{
versionKey: false,
id: false,
toJSON: {
virtuals: true
},
toObject: {
virtuals: true
}
}
);
const FAQ = mongoose.model('faq', FAQSchema);
const test = new FAQ({
question: 'question',
title: 'title',
answer: 'answer',
importance: 250,
tags: ['one', 'two', 'three']
});
async function run() {
assert.strictEqual(mongoose.version, '5.9.2');
await mongoose.connect(URI, OPTS);
await connection.dropDatabase();
await FAQ.create(test);
const found = await FAQ.find({}).exec();
assert.deepEqual(found[0].tags, ['one', 'two', 'three']);
console.log('All Assertions Pass.');
await connection.close();
}
run();
Shell Output
➜ issues ./8615.js
All Assertions Pass.
➜ issues
so when I ran your sample code it worked as well which I guess is a good sign..
After reading more about it, I removed the versionKey=false line but that didn't do anything either.
I added a console.log to my FAQ.schema and got the following output:
Schema {
obj: {
question: [Function: String],
title: [Function: String],
answer: [Function: String],
importance: { type: [Function: Number], default: 500 },
tags: { type: [Array], index: true }
},
paths: {
question: SchemaString {
enumValues: [],
regExp: null,
path: 'question',
instance: 'String',
validators: [],
getters: [],
setters: [],
options: [SchemaStringOptions],
_index: null,
[Symbol(mongoose#schemaType)]: true
},
title: SchemaString {
enumValues: [],
regExp: null,
path: 'title',
instance: 'String',
validators: [],
getters: [],
setters: [],
options: [SchemaStringOptions],
_index: null,
[Symbol(mongoose#schemaType)]: true
},
answer: SchemaString {
enumValues: [],
regExp: null,
path: 'answer',
instance: 'String',
validators: [],
getters: [],
setters: [],
options: [SchemaStringOptions],
_index: null,
[Symbol(mongoose#schemaType)]: true
},
importance: SchemaNumber {
path: 'importance',
instance: 'Number',
validators: [],
getters: [],
setters: [],
options: [SchemaNumberOptions],
_index: null,
defaultValue: 500,
[Symbol(mongoose#schemaType)]: true
},
tags: SchemaArray {
schemaOptions: [Object],
casterConstructor: [Function],
caster: [SchemaString],
'$embeddedSchemaType': [SchemaString],
'$isMongooseArray': true,
path: 'tags',
instance: 'Array',
validators: [],
getters: [],
setters: [Array],
options: [SchemaArrayOptions],
_index: true,
defaultValue: undefined,
[Symbol(mongoose#schemaType)]: true
},
_id: ObjectId {
path: '_id',
instance: 'ObjectID',
validators: [],
getters: [],
setters: [Array],
options: [SchemaObjectIdOptions],
_index: null,
defaultValue: [Function],
[Symbol(mongoose#schemaType)]: true
},
deletedAt: SchemaDate {
path: 'deletedAt',
instance: 'Date',
validators: [],
getters: [],
setters: [],
options: [SchemaDateOptions],
_index: true,
[Symbol(mongoose#schemaType)]: true
},
__v: SchemaNumber {
path: '__v',
instance: 'Number',
validators: [],
getters: [],
setters: [],
options: [SchemaNumberOptions],
_index: null,
[Symbol(mongoose#schemaType)]: true
}
},
aliases: {},
subpaths: {
'tags.$': SchemaString {
enumValues: [],
regExp: null,
path: 'tags.$',
instance: 'String',
validators: [],
getters: [],
setters: [],
options: SchemaStringOptions {},
_index: null,
[Symbol(mongoose#schemaType)]: true
}
},
virtuals: {},
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [],
_indexes: [],
methods: {
post: [Function: post],
put: [Function: put],
patch: [Function: patch],
del: [Function: del],
fakeOnly: [Function: instanceFakeOnly],
fakeExcept: [Function: instanceFakeExcept],
tag: [Function: tag],
untag: [Function: untag]
},
methodOptions: {},
statics: {
path: [Function: path],
seed: [Function: seedModel],
clearAndSeed: [Function: clearAndSeedModel],
getById: [Function: getById],
_get: [Function: _get],
get: [Function: get],
fresh: [Function: fresh],
post: [Function: post],
put: [Function: put],
patch: [Function: patch],
del: [Function: del],
fake: [Function: fake],
fakeOnly: [Function: fakeOnly],
fakeExcept: [Function: fakeExcept],
TAGGABLE_FIELDS: {},
SEARCHABLE_FIELDS: [ 'tags' ],
search: [Function: search],
AGGREGATABLE_FIELDS: {},
lookup: [Function: lookup]
},
tree: {
question: [Function: String],
title: [Function: String],
answer: [Function: String],
importance: { type: [Function: Number], default: 500 },
tags: {
type: [Array],
index: true,
duplicate: false,
searchable: true,
exportable: false,
hide: true,
default: undefined
},
_id: { auto: true, type: 'ObjectId' },
deletedAt: { type: [Function: Date], index: true },
__v: [Function: Number]
},
query: {},
childSchemas: [],
plugins: [
{ fn: [Function], opts: [Object] },
{ fn: [Function], opts: [Object] },
{ fn: [Function], opts: [Object] },
{ fn: [Function], opts: [Object] },
{ fn: [Function: path], opts: undefined },
{ fn: [Function: handleUniqueError], opts: undefined },
{ fn: [Function: exports], opts: undefined },
{ fn: [Function: path], opts: undefined },
{ fn: [Function: handleUniqueError], opts: undefined },
{ fn: [Function: exports], opts: undefined },
{ fn: [Function: restActions], opts: undefined },
{ fn: [Function], opts: [Object] }
],
'$id': 6,
s: { hooks: Kareem { _pres: [Map], _posts: [Map] } },
_userProvidedOptions: {
id: false,
toJSON: { getters: false, virtuals: true, transform: [Function] },
toObject: { getters: false, virtuals: true, transform: [Function] }
},
options: {
id: false,
toJSON: { getters: false, virtuals: true, transform: [Function] },
toObject: { getters: false, virtuals: true, transform: [Function] },
typePojoToMixed: true,
typeKey: 'type',
noVirtualId: false,
_id: true,
noId: false,
validateBeforeSave: true,
read: null,
shardKey: null,
autoIndex: null,
minimize: true,
discriminatorKey: '__t',
versionKey: '__v',
capped: false,
bufferCommands: true,
strict: true,
pluralization: true
},
'$globalPluginsApplied': true
}
When I run it with your sample program I get:
Schema {
obj: {
question: [Function: String],
title: [Function: String],
answer: [Function: String],
importance: { type: [Function: Number], default: 500 },
tags: { type: [Array], index: true }
},
paths: {
question: SchemaString {
enumValues: [],
regExp: null,
path: 'question',
instance: 'String',
validators: [],
getters: [],
setters: [],
options: [SchemaStringOptions],
_index: null,
[Symbol(mongoose#schemaType)]: true
},
title: SchemaString {
enumValues: [],
regExp: null,
path: 'title',
instance: 'String',
validators: [],
getters: [],
setters: [],
options: [SchemaStringOptions],
_index: null,
[Symbol(mongoose#schemaType)]: true
},
answer: SchemaString {
enumValues: [],
regExp: null,
path: 'answer',
instance: 'String',
validators: [],
getters: [],
setters: [],
options: [SchemaStringOptions],
_index: null,
[Symbol(mongoose#schemaType)]: true
},
importance: SchemaNumber {
path: 'importance',
instance: 'Number',
validators: [],
getters: [],
setters: [],
options: [SchemaNumberOptions],
_index: null,
defaultValue: 500,
[Symbol(mongoose#schemaType)]: true
},
tags: SchemaArray {
schemaOptions: [Object],
casterConstructor: [Function],
caster: [SchemaString],
'$embeddedSchemaType': [SchemaString],
'$isMongooseArray': true,
path: 'tags',
instance: 'Array',
validators: [],
getters: [],
setters: [],
options: [SchemaArrayOptions],
_index: true,
defaultValue: [Function],
[Symbol(mongoose#schemaType)]: true
},
_id: ObjectId {
path: '_id',
instance: 'ObjectID',
validators: [],
getters: [],
setters: [Array],
options: [SchemaObjectIdOptions],
_index: null,
defaultValue: [Function],
[Symbol(mongoose#schemaType)]: true
}
},
aliases: {},
subpaths: {
'tags.$': SchemaString {
enumValues: [],
regExp: null,
path: 'tags.$',
instance: 'String',
validators: [],
getters: [],
setters: [],
options: SchemaStringOptions {},
_index: null,
[Symbol(mongoose#schemaType)]: true
}
},
virtuals: {},
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [],
_indexes: [],
methods: {},
methodOptions: {},
statics: {},
tree: {
question: [Function: String],
title: [Function: String],
answer: [Function: String],
importance: { type: [Function: Number], default: 500 },
tags: { type: [Array], index: true },
_id: { auto: true, type: 'ObjectId' }
},
query: {},
childSchemas: [],
plugins: [
{ fn: [Function], opts: [Object] },
{ fn: [Function], opts: [Object] },
{ fn: [Function], opts: [Object] },
{ fn: [Function], opts: [Object] },
{ fn: [Function], opts: [Object] }
],
'$id': 1,
s: { hooks: Kareem { _pres: [Map], _posts: [Map] } },
_userProvidedOptions: {
versionKey: false,
id: false,
toJSON: { virtuals: true },
toObject: { virtuals: true }
},
options: {
versionKey: false,
id: false,
toJSON: { virtuals: true },
toObject: { virtuals: true },
typePojoToMixed: true,
typeKey: 'type',
noVirtualId: false,
_id: true,
noId: false,
validateBeforeSave: true,
read: null,
shardKey: null,
autoIndex: null,
minimize: true,
discriminatorKey: '__t',
capped: false,
bufferCommands: true,
strict: true,
pluralization: true
},
'$globalPluginsApplied': true,
_requiredpaths: [],
_indexedpaths: [ [ [Object], [Object] ] ]
}
what stuck out on my first glance was the hide: true on the schema.tree.tags property
Issue seems related to the mongoose-rest-actions plugin, if I disable that plugin the tags start to work.
I'm assuming it's because mongoose-rest-actions uses mongoose-taggable, which probably utilizes the property 'tags' as well, causing that plugin to conflict with your schema
Yeah this sounds like a plugin is setting an unexpected option on the tags property. Try using a different property name :+1: