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.