如何在猫鼬的数组内填充?

时间:2018-07-31 17:54:37

标签: javascript express mongoose

我正在尝试显示朋友组中的用户。我用猫鼬和快递。这是朋友组架构:

    var mongoose = require("mongoose");

    var FriendGroupSchema = new mongoose.Schema({
        name: String,
        public_or_private: { type: String, enum: ["public", "private",] },
        friends: [
            {
                user: {
                    type: mongoose.Schema.Types.ObjectId,
                    ref: "friend"
                },
                admin: Boolean
            }
        ]
    });

    module.exports = mongoose.model("friend_group", FriendGroupSchema);

这是用户架构:

var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");

var UserSchema = new mongoose.Schema({
    username: String,
    password: String,
    profile_picture: String,
    about: String,
    friend_groups: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "friend_group"
        }
    ],
    friends: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "friend"
        }
    ],
});

UserSchema.plugin(passportLocalMongoose);

module.exports = mongoose.model("User", UserSchema);

我需要在用户的显示页面上显示朋友组内的用户。我需要先填充到用户中,然后填充到朋友组中,然后填充到friends数组中,最后填充用户以及他/她是否是管理员(我需要同时显示两者)。
这是我到目前为止的内容:

.populate({path:"friend_groups", populate: {path: 'friends', populate: {path: 'user', model: 'User'}}})

但是这似乎不起作用。我在做什么错了?

0 个答案:

没有答案