Mongodb $ lookup动态集合 [英] Mongodb $lookup dynamic collection

查看:80
本文介绍了Mongodb $ lookup动态集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模式,其中项目类型可能有所不同,并在connections.kind中提到.

I have following schema where the item type might vary, and is mentioned in connections.kind.

var userSchema = new Schema({
  name: String,
  connections: [{
    kind: String,
    item: { type: ObjectId, refPath: 'connections.kind' }
  }]
});

var organizationSchema = new Schema({ name: String });

我正在尝试进行动态查找,以便填充item对象.但这似乎不起作用.

I am trying to do a dynamic lookup so that the item object is populated. But this doesn't seem to work.

db.users.aggregate([
    {
        $lookup:{
            from: '$connections.kind',
            localField: 'connections.item',
            foreignField: '_id',
            as: 'items'
        }
    }
])

我知道我可以使用 mongoose.populate 做到这一点,但想知道是否$ lookup可以实现

I know I can do it with mongoose.populate, but want to know if it is possible with $lookup

推荐答案

到目前为止,您还不能这样做. from字段不能是表达式,并且必须是字符串文字.但是,您可以在这里跟踪一个未解决的问题,该问题似乎恰恰是您所需要的: https: //jira.mongodb.org/browse/SERVER-22497 .

As of now, you can't. The from field cannot be an expression and must be a string literal. However, there is an open issue which you can track here that appears to be exactly what you need: https://jira.mongodb.org/browse/SERVER-22497.

这篇关于Mongodb $ lookup动态集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆