mongoose populate查询没有显示所需的结果

时间:2017-02-05 23:15:01

标签: mongodb mongoose mongoose-populate

我是MongoDB和Mongoose的新手,我遇到了一些问题,更具体地说,我遇到填充问题,问题显示在下面。

我的架构:

'use strict';

    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;

    /* ------------------------------------- Field config ---------------------------------------- */
    const UsersSchema = new Schema({
          name: {
           type: String,
           required: true,
           min: 3,
           set: str => str.replace(/\b\w/g, l => l.toUpperCase())
         },
          lastname: {
           type: String,
           required: true,
           min: 3,
           set: str => str.replace(/\b\w/g, l => l.toUpperCase())
         },
          email: {
           type: String,
           required: true,
           unique: true,
           validade: {
             validator: str => regExp.test(str)
           }
          },
          password: {
           type: String,
           required: true,
           min: 8
          },
          fcm:{ 
            type: String,
            required: true
          },
          birthDate: {
           type: Date,
           required: true,
           get: convertToBR
          },
          gender: {
            type: String,
            enum: ['M', 'F'],
            required: true
          },
          profileImage: {
            type: String,
            required: false,
            get: alterUri
          },
          aboutMe: {
           type: String,
           max: 500
          },
          enabled: {
           type: Boolean,
           required: true,
           default: true
          },
          createdAt: {
            type: Date,
            required: true,
            default: new Date()
          },
          token: {
            type: String,
            required: true,
            index: true
          },
          locations: {
           type: [mongoose.Schema.Types.ObjectId],
           require: false,
           ref: 'Location'
          },
          lastVisitants: {
           type: [{
             user: mongoose.Schema.Types.ObjectId,
             createAt: {
               type: Date,
               required: true,
               default: new Date()
            }
           }],
           required: false,
           ref: 'Users'
         }
    }, {
          toObject: {
              virtuals: true,
              getters: true,
              setters: true
          },
          toJSON: {
              virtuals: true,
              getters: true,
              setters: true
          }
    });

    //Code bellow removed for make to easy know

MongoDB中生成的文档:

{
    "_id": "589288de533c9555163cf263",
    "email": "luiz@sene.com",
    "password": "CQziXoB6XrBFBTWe5s/kFsIGYqbtDPBBKQgADUZF9co=",
    "name": "luiz Fernando",
    "lastname": "Sene",
    "birthDate": "1988-08-11T00:00:00.000Z",
    "gender": "M",
    "fcm": "34567890987654345678",
    "token": "58979c08b048917ed8b434ac",
    "createdAt": "2017-02-02T01:17:38.168Z",
    "enabled": true,
    "__v": 0,
    "aboutMe": "algo sobre mim aqui mudar",
    "locations": [
        "5893e2e0c8a01b4ed21c8c39",
        "5893e305c8a01b4ed21c8c3a",
        "5893e32ac8a01b4ed21c8c3b",
        "5893e34dc8a01b4ed21c8c3c",
        "5893e92838bd205ba42c2c8a",
        "5893ea888628c45d2bc6683a"
    ],
    "profileImage": "images/589288de533c9555163cf263/1486330852976.png",
    "lastVisitants": [
        {
            "createAt": "2017-02-05T19:23:17.697Z",
            "_id": "58977ba99fc2b7485dbdbf26",
            "user": "589778e22a9dd5449e4f92df"
        }
    ]
}

当我尝试填充locationslastVisitants时会出现问题。

我的咨询如下:

UsersSchema.findOne({_id: data.data.id}, {lastVisitants: true, locations: true})
            .populate({
                path: 'locations',
                model: 'Location',
                select: 'description',
                options: {
                    limit: 1,
                    sort: {
                        createdAt: -1
                    }
                }
            })
            .populate({
                path: 'lastVisitants.user',
                model: 'Users',
                select: '_id lastVisitants',
                options: {
                    limit: 5
                    //TODO: Add sort by createdAt
                }
            })
            .exec((err, result) => {
                if (err) {
                    Utils.callback(err, null, res);
                    return;
                }

                Utils.callback(null, result, res);
            });

咨询结果:

{
  "_id": "589288de533c9555163cf263",
  "lastVisitants": [
    {
      "_id": "58977c1c9fc2b7485dbdbf27",
      "user": {
        "_id": "589778e22a9dd5449e4f92df",
        "lastVisitants": [
          {
            "_id": "58977c1c9fc2b7485dbdbf27",
            "user": "589288de533c9555163cf263",
            "createAt": "2017-02-05T19:23:17.697Z"
          }
        ],
        "profileImage": "http://192.168.0.19:3000/undefined",
        "birthDate": {
          "en": "NaN-NaN-NaN",
          "br": "NaN/NaN/NaN"
        },
        "fullname": "undefined undefined",
        "age": null,
        "id": "589778e22a9dd5449e4f92df"
      },
      "createAt": "2017-02-05T19:23:17.697Z"
    }
  ],
  "profileImage": "http://192.168.0.19:3000/undefined",
  "birthDate": {
    "en": "NaN-NaN-NaN",
    "br": "NaN/NaN/NaN"
  },
  "fullname": "undefined undefined",
  "age": null,
  "id": "589288de533c9555163cf263"
}

预期结果:

{
    "_id": "589288de533c9555163cf263",
    "locations":[
  {
    "_id": "5893ea888628c45d2bc6683a",
    "description": "Cruzeiro - SP, Brasil"
  }
    ]
    "lastVisitants": [
        {
            _id: ""
            user: {
                "id": "58977ba99fc2b7485dbdbf26",
                "fullname": "Fábio Pereira",
                "profileImage": "http://192.168.0.19:3000/images/589288de533c9555163cf263/1486319528451.jpeg",
                "gender": "M",
                // more fields ...
            },
            createdAt: ""
        }
    ]
}

我的问题是如何才能使这个查询带来我真正想要的东西?

0 个答案:

没有答案