mongodb的populate嵌套使用问题

有如下两个Model

(1)用户Model

var usersSchema = new Schema({
    f_id: {
        unique: true,
        type: String
    },
    f_name: String
})

(2)记录Model

var batchMainSchema = new Schema({
        f_id: {
            unique: true,
            type: String
        },
        f_user_log: [{
            f_option: String,
            f_time: Date,
            f_user_model: { type: Schema.Types.ObjectId, ref: "users" },
        }]
    })

需要在记录Model中的f_user_log字段中保存用户操作记录

请问这种写法能不能在查询记录数据时获取到f_user_model的值

如下图中的写法不能获取到想要的用户信息

clipboard.png

请问大伙有没有相应的解决办法,谢谢!!!

阅读 2.7k
2 个回答

populate做不到,用aggregate

新手上路,请多包涵

.populate({path: 'f_user_log. f_user_model'})

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题