0
[
  {
    _id: 555,
    names:['John','Doe','David']
  },
  {
    _id: 625,
    names:['David','Mark','Carl']
  },
  {
    _id: 299,
    names:['Bill','Carlos','Ventus']
  }
]

How can I return only the names(object) of all the object having _id in MongoDB ? Please help me..

Ashh
  • 44,693
  • 14
  • 105
  • 132
  • 2
    Possible duplicate of [How to select a single field in MongoDB?](https://stackoverflow.com/questions/25589113/how-to-select-a-single-field-in-mongodb) – Ashh Jul 03 '18 at 11:06
  • https://stackoverflow.com/questions/9548186/mongoose-use-of-select-method – Simran Jul 03 '18 at 11:07
  • http://mongoosejs.com/docs/queries.html try to use `select` – Harish Jul 03 '18 at 11:31

1 Answers1

0
model.find({_id : { $exists : 1} }, "-_id names", { lean : true })

As each document in mongoDB has _id : model.find({}, "-_id names", { lean : true }) this would be fine.

You could also use mongo aggregate as follows :

model.aggregate({ $project : { _id : 0, names : 1 })
Prajval M
  • 2,298
  • 11
  • 32