Skip to content

MongooseDocument { undefined } and doc.someProp #9085

Closed
@thernstig

Description

@thernstig

Do you want to request a feature or report a bug?

Feature? Bug?

What is the current behavior?

I have a document field like:

var parentSchema = new Schema({
  time: Date,
  child:  {
    prop1: String,
    prop2: String,
  }
}

According to https://mongoosejs.com/docs/subdocs.html#altsyntaxsingle this should not work since I do not have { typePojoToMixed: false }.

Also, in the example in that link above it also shows that type: is needed (is that correct that it is needed?)

But it does work in a sense, or I am confused on what is supposed to happen according to the docs that says it is a mixed type instead.

I query (findOne) and get a document that has only filled in the date. When I then do if (doc.child) it returns true. Checking console.log(doc.child) gives MongooseDocument { undefined }.

Is the intention here that I need to do doc.child.toObject() (or lean() on the query) to have this work? Or is there a way mongoose could instead return undefined subdocs as undefined instead?

The reason I am asking is that this caused a bug in the above if check where the subdoc was undefined, but returned truthy since it was gave MongooseDocument { undefined }.

What is the expected behavior?

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Mongoose 5.9.13
Node.js 12.15.0
MongoDB 3.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsThis issue is due to a mistake or omission in the mongoosejs.com documentation

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions