0

I'm using Mongoose with NodeJs And the current document in mongoDB like this:

{
   "_id":"5cc26b81ea0b841b2a8723a1",
   "lastestEdit":"2019-05-06T10:23:27.777Z",
   "name":"linh",
   "contact":"0923123",
   "dob":"2019-04-11T17:00:00.000Z",
   "gender":"M",
   "id":"PW3933059352",
   "oldID":1004144,
   "email":"linh@gmail.com",
   "created":"2019-04-26T02:22:56.978Z",
   "images":[
      {
         "_id":"5cd00a3b265a2967d87b94cf",
         "name":"PW3933059352-20190506-linh-0923123-1557137979",
         "treatment":"beauty",
         "childFolder":[
            {
               "_id":"5cd00a3b265a2967d87b94d2",
               "name":"Trước",
               "googleDriveChildFolderId":"1i4qa8Z8KDdURYQ74Yho75TA4o3zjfHXl"
            },
            {
               "_id":"5cd00a3b265a2967d87b94d1",
               "name":"Sau",
               "googleDriveChildFolderId":"14N2YSxgeYUO4XMxmztzXNaaSGn5QoqAj"
            },
            {
               "_id":"5cd00a3b265a2967d87b94d0",
               "name":"X-Quang",
               "googleDriveChildFolderId":"1X-O0pcmpv5gQ_yJ2KHG0IYAn4tsqGOAC"
            },
            {
               "_id":"5cd00a43265a2967d87b94d3",
               "name":"test"
            }
         ],
         "gTreatmentId":"1yWgO-wplDLldCweOlhHNggAssPsD1TJ0",
         "gPatientFolderId":"1B27Sw0a1NbOs5J4pzs2PhNBjR9RUZ4-N"
      },
      {
         "_id":"5cd00b1c265a2967d87b94d4",
         "name":"PW3933059352-20190506-linh-0923123-1557138204",
         "treatment":"tooth_removal",
         "childFolder":[
            {
               "_id":"5cd00b1c265a2967d87b94d7",
               "name":"Trước",
               "googleDriveChildFolderId":"1OBZ0nBzmgJyOi--RvTKtguJL_db4x2d6"
            },
            {
               "_id":"5cd00b1c265a2967d87b94d6",
               "name":"Sau",
               "googleDriveChildFolderId":"1IXqsbk_EwrNLFupGf8WUrT1uxfi_ZG-K"
            },
            {
               "_id":"5cd00b1c265a2967d87b94d5",
               "name":"X-Quang",
               "googleDriveChildFolderId":"1mwSHY9L0ZKHCL9bXefGADSDw9U3XpMhG"
            }
         ],
         "gTreatmentId":"1Z-ogjXNrP4Y60f8fAlTsFKuwQXII5cQo",
         "gPatientFolderId":"1FJWS9R6QlPvktEhTsfofaQpWeWO2BQH4"
      }
   ],
   "address":"hanoi",
   "job":"Sinh viên",
   "__v":170,
   "lastestAppointment":null
}

I'm trying to use mongoose findOne query and i would like to get 1 field images.gTreatmentId where images._id = 5cd00a3b265a2967d87b94cf

Here is my function to query:

const gTreatmentFolderId = await Patient.findOne(
        {
          'images._id': id,
        }
      )
      .populate({
        path: 'images',
        select: 'gTreatmentId'
      })
return gTreatmentFolderId

where in the param id = 5cd00a3b265a2967d87b94cf

in the return i got entire objects instead of just the gTreatmentId field

Am i doing something wrong ?

Linh Nguyen
  • 3,452
  • 4
  • 23
  • 67
  • 2
    The `select` is not the problem but that that `images._id` does not exist as something you can query until **after** you perform the `populate()`. This is if you actually **need** the populate. If the document actually looks like it does in the question ( within a single collection and no `populate()` required to make it look like that ) then instead you are "Retrieving only the queried element in an array", and that's a different thing again, which actually has nothing to do with `populate()` – Neil Lunn May 06 '19 at 10:35
  • you can use projection https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/ – Prabhat Mishra May 06 '19 at 10:41

0 Answers0