猫鼬select()与find()不会返回数组中的特定字段数组

时间:2019-05-03 07:18:30

标签: arrays node.js mongoose subdocument

我目前正在将nodeJS与mongoose一起使用 我有这样的患者模式

     var patientSchema = mongoose.Schema({
            name: {
                type: String,
                required: true,
            },
            contact: {
                type: String,
                required: true
            },
            dob: {
                type: Date
            },
            id: {
                type: String,
                required: true
            },
            gender: {
                type: String
            },
            email: {
                type: String
            },
            payments: {

            },
            diagnoses: {

            },
            prescriptions: {

            },
            appointments: {

            },
            job: {
                type: String,
                default: "Unknown"
            },
            address: {
                type: String,
                default: ""
            },
            images: [{
                name: String,
                gPatientFolderId: String,
                treatment: String,
                gTreatmentId: String,
                childFolder: [{    //<- i would like this to output
                    name: String,
                    googleDriveChildFolderId: String
                }]
            }],
            oldID: {
                type: Number,
                unique: true
            },
            created: {
                type: Date
            },
            lastestEdit: {
                type: Date
            },
            lastestAppointment: {
                type: Appointment.schema
            },
            defaultLocation: {
                type: String,
            },
            lastestRecord: {
                type: mongoose.Schema.Types.ObjectId
            }
        }, {
                usePushEach: true,
            });

var Patient = mongoose.model('patients', patientSchema);

im尝试在此函数中将childFolder数组打印为json类型的数组

exports.addGChildFolderId = (patientId, folderNameID, childFolderName, childFolderId) => {
    Patient.find(
        {
            '_id': patientId,
            'images.gPatientFolderId': folderNameID,
        }
    ).select('images.$.childFolder -_id').lean().exec(function (err, docs) {
        console.log('%j', docs)
    });

}

输入的是患者_id(患者ID)和图像gPatientFolderId(folderNameID)

我想console.log一个特定的childFolder json数组,所以我将find()与select()和lean()一起使用

当前正在使用的文档为例:

{  
   "_id":"5cc26b81ea0b841b2a8723a1",
   "lastestEdit":"2019-05-03T07:02:53.251Z",
   "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":[  
      {  
         "name":"PW3933059352-20190503-linh-0923123-1556866574",
         "treatment":"beauty",
         "_id":"5ccbe60e47dcfc3b110236fd",
         "childFolder":[  
            {  
               "name":"Trước",
               "_id":"5ccbe60e47dcfc3b11023700"
            },
            {  
               "name":"Sau",
               "_id":"5ccbe60e47dcfc3b110236ff"
            },
            {  
               "name":"X-Quang",
               "_id":"5ccbe60e47dcfc3b110236fe"
            }
         ],
         "gTreatmentId":"15Rs5Sj3TlfWG3cdPQXe8RwKLtJEJYyMG",
         "gPatientFolderId":"1B9xGcl3JjesaIch2IH9paAMZ2H_I1Y2d"
      },
      {  
         "name":"PW3933059352-20190503-linh-0923123-1556866603",
         "treatment":"tooth_removal",
         "_id":"5ccbe62b8d5de53b3ddb8973",
         "childFolder":[  
            {  
               "name":"Trước",
               "_id":"5ccbe62b8d5de53b3ddb8976"
            },
            {  
               "name":"Sau",
               "_id":"5ccbe62b8d5de53b3ddb8975"
            },
            {  
               "name":"X-Quang",
               "_id":"5ccbe62b8d5de53b3ddb8974"
            }
         ],
         "gTreatmentId":"1ZcVv5mwDPtQBnbrUCSVPnv3U3vcN0wF6",
         "gPatientFolderId":"1yVUFmQfR3suEBcJFmeNbbZIjSJJzoYsE"
      },
      {  
         "name":"PW3933059352-20190503-linh-0923123-1556866628",
         "treatment":"invisalign_invitechnager",
         "_id":"5ccbe644b6ad703b69cb01cc",
         "childFolder":[  
            {  
               "name":"Trước",
               "_id":"5ccbe644b6ad703b69cb01cf"
            },
            {  
               "name":"Sau",
               "_id":"5ccbe644b6ad703b69cb01ce"
            },
            {  
               "name":"X-Quang",
               "_id":"5ccbe644b6ad703b69cb01cd"
            }
         ],
         "gTreatmentId":"1GGiWRylvJ-QE3c1sIWupg8DQr8p1ktaP",
         "gPatientFolderId":"1RA_KeAQDmeUYWbpx1kcBvOcr8xiaxXmn"
      },
      {  
         "name":"PW3933059352-20190503-linh-0923123-1556866676",
         "treatment":"gummy_smile_surgery",
         "_id":"5ccbe674472b0b3b9a221cd7",
         "childFolder":[  
            {  
               "name":"Trước",
               "_id":"5ccbe674472b0b3b9a221cda"
            },
            {  
               "name":"Sau",
               "_id":"5ccbe674472b0b3b9a221cd9"
            },
            {  
               "name":"X-Quang",
               "_id":"5ccbe674472b0b3b9a221cd8"
            }
         ],
         "gTreatmentId":"1UQoz3fvsPcOKl-rt_k5KHOw_hG00rmAO",
         "gPatientFolderId":"12mNoT2m3IEpU8C7_dqbw9icrynjO12fH"
      },
      {  
         "name":"PW3933059352-20190503-linh-0923123-1556866972",
         "treatment":"orthodontic",
         "_id":"5ccbe79cde26d23c7f15abde",
         "childFolder":[  
            {  
               "name":"Trước",
               "_id":"5ccbe79cde26d23c7f15abe1"
            },
            {  
               "name":"Sau",
               "_id":"5ccbe79cde26d23c7f15abe0"
            },
            {  
               "name":"X-Quang",
               "_id":"5ccbe79cde26d23c7f15abdf"
            }
         ],
         "gTreatmentId":"1v9WE4Z_C9WK-o3FGghLVn-Nju7RkHtd2",
         "gPatientFolderId":"133NoFIFyA9M71HlefynRTfuxTd3rsE1H"
      }
   ],
   "address":"hanoi",
   "job":"Sinh viên",
   "__v":142,
   "lastestAppointment":null
}

我目前从该函数获得的输出:

[
   {
      "images":[
         {
            "name":"PW3933059352-20190503-linh-0923123-1556866972",
            "treatment":"orthodontic",
            "_id":"5ccbe79cde26d23c7f15abde",
            "childFolder":[
               {
                  "name":"Trước",
                  "_id":"5ccbe79cde26d23c7f15abe1"
               },
               {
                  "name":"Sau",
                  "_id":"5ccbe79cde26d23c7f15abe0"
               },
               {
                  "name":"X-Quang",
                  "_id":"5ccbe79cde26d23c7f15abdf"
               }
            ],
            "gTreatmentId":"1v9WE4Z_C9WK-o3FGghLVn-Nju7RkHtd2",
            "gPatientFolderId":"133NoFIFyA9M71HlefynRTfuxTd3rsE1H"
         }
      ]
   }
]

它仍然可以打印出具有所有属性的图像,但是我只想要childFolder数组。

我应该如何修复该功能?

0 个答案:

没有答案