Mongodb如何获取其他集合中不存在的数据

Irs*_*haq 1 mongoose mongodb nosql mongodb-query

我有两个收藏usertransaction. 我想要获取user不在exists集合中的记录transaction。然后我就这样做


[
    {
       "$lookup" : {
            "from" : "transaction",
            "localField":"_id",
            "foreignField":"user",
            "as" : "trans"
       }
   },
   {
       "$match": { "transaction.user": { "$exists": false } }
   },
   {"$limit":20}
]

Run Code Online (Sandbox Code Playgroud)

但我没有记录我想要的东西。

小智 13

聚合自usertransaction集合。正如 @thammada.ts 所提到的,你应该trans$match这样使用

db.user.aggregate([
  {
    "$lookup": {
      "from": "transaction",
      "localField": "_id",
      "foreignField": "user",
      "as": "trans"
    }
  },
  {
    "$match": {
      "trans.user": {
        "$exists": false
      }
    }
  }
])
Run Code Online (Sandbox Code Playgroud)

蒙戈游乐场

_id还要验证集合中user和集合user 中两个字段的类型transaction