2

I have the following data in my MongoDB, modeled via my Person model:

 { _id: 135, name: 'Alfie', age: 26 }
 { _id: 217, name: 'Ronny', age: 34 }
 { _id: 400, name: 'Sandy', age: 45 }
 { _id: 676, name: 'William', age: 24 }
 { _id: 987, name: 'Debra', age: 31 }
 { _id: 356, name: 'Kevin', age: 47 }

Now I run the following query:

const findQuery = Person.find({ _id: { $lt: 300 } }).select({ name: 1 })

findQuery.exec().then(doc => {
  for (let person of doc) {
    console.log(person)
    console.log(person._id)
    console.log(person.name)
  }
}

The output is:

{ _id: 135, name: 'Alfie' }
135
undefined
{ _id: 217, name: 'Ronny' }
217
undefined

My question is, why is the string contained within person.name return undefined? where as the object itself and person._id is returned correctly.

1 Answer 1

2

I found the answer, name was missing from mongoose.Schema, so it couldn't find the value, even if it was present in the database.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.