Mongodb $查找dynamic集合
我有以下架构的项目types可能会有所不同,并在connections.kind
提到。
var userSchema = new Schema({ name: String, connections: [{ kind: String, item: { type: ObjectId, refPath: 'connections.kind' } }] }); var organizationSchema = new Schema({ name: String });
我正在试图做一个dynamic的查找,以便项目对象被填充。 但是这似乎不起作用。
db.users.aggregate([ { $lookup:{ from: '$connections.kind', localField: 'connections.item', foreignField: '_id', as: 'items' } } ])
我知道我可以用mongoose.populate
做,但是想知道是否可以用$ lookup
截至目前,你不能。 from
字段不能是expression式,并且必须是string文字。 不过,您可以在这里find一个开放的问题,看起来正是您所需要的: https : //jira.mongodb.org/browse/SERVER-22497 。