合并两个集合并在mongodb中比较两个字段值

时间:2019-05-15 07:03:02

标签: node.js mongodb express

我想基于比较两个字段值来合并两个集合

收藏一:

{
  "_id" : ObjectId("5cd27577732093378c0a760c"),
  "level" : "Safety"
}

收藏二:

{
  "_id" : ObjectId("5c653ffb58ff1515c0a511a5"),
  "course_id" : "1",
  "c_name" : "First Aid",
  "c_level" : "5cd27577732093378c0a760c", 
  "last_update" : "May 14 2019"
}

我想通过比较集合1的_id与集合2的c_level来合并上述集合值

  db.one .aggregate([
   {
    $lookup:
   {
   from: "two ",
   localField: "c_level",
   foreignField: "level",
   as: "docs"
   }
   } 
   ]) 

这显示空值

1 个答案:

答案 0 :(得分:0)

您已将localFieldforeignField切换了(还有一个小改动):

$lookup:
   {
      from: "two ",
      localField: "_id",
      foreignField: "c_level",
      as: "docs"
   }

假设c_level的类型为ObjectId