MongoDB $lookup Objectid get empty array?

后端 未结 1 1566
夕颜
夕颜 2020-12-07 05:33

I\'m new to mongodb and practice aggregate with $lookup in mongoose following document, And i met a question really confusing.

I have user

相关标签:
1条回答
  • 2020-12-07 06:16

    The from field in $lookup is the collection name, not a model variable name. So if you are initializing the model like this

    db.model('User', userSchema)
    

    then the lookup query should be

    commentModel.aggregate([{$lookup: {
      from: 'users',
      localField: 'author',
      foreignField: '_id',
      as: 'common'
    }])
    
    0 讨论(0)
提交回复
热议问题