Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

mongodb $lookup – suppress non-matching documents

I only want to see matching documents, i.e. only T3 in the example below. I can find the matching documents between lotterytickets (many documents) and lotterydrawing (only a few documents).

How can I filter out the non-matching documents? Basically, I’d not like to see documents with the condition drawnticket==[], but I haven’t found the conditional code to apply.

Any help would be appreciated. Thank you in advance

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Configuration:

db={
  "lotteryticket": [
    {
      "_id": ObjectId("6021ce0cb4d2c2b4f24c3a2e"),
      "ticket": "T1",
      "player": "Alice"
    },
    {
      "_id": ObjectId("6021ce0cb4d2c2b4f24c3a2f"),
      "ticket": "T2",
      "player": "Bob"
    },
    {
      "_id": ObjectId("6021ce0cb4d2c2b4f24c3a33"),
      "ticket": "T3",
      "player": "Charles"
    }
  ],
  "lotterydrawing": [
    {
      "_id": ObjectId("63309480b749b733c087b758"),
      "ticket": "T3"
    },
    {
      "_id": ObjectId("63309480b749b733c087b759"),
      "ticket": "T9"
    },
    {
      "_id": ObjectId("63309480b749b733c087b75a"),
      "ticket": "T77"
    }
  ]
}

Query:

db.lotteryticket.aggregate([
  {
    $lookup: {
      from: "lotterydrawing",
      localField: "ticket",
      foreignField: "ticket",
      as: "drawnticket",
      
    }
  }
])

Result:

[
  {
    "_id": ObjectId("6021ce0cb4d2c2b4f24c3a2e"),
    "drawnticket": [],
    "player": "Alice",
    "ticket": "T1"
  },
  {
    "_id": ObjectId("6021ce0cb4d2c2b4f24c3a2f"),
    "drawnticket": [],
    "player": "Bob",
    "ticket": "T2"
  },
  {
    "_id": ObjectId("6021ce0cb4d2c2b4f24c3a33"),
    "drawnticket": [
      {
        "_id": ObjectId("63309480b749b733c087b758"),
        "ticket": "T3"
      }
    ],
    "player": "Charles",
    "ticket": "T3"
  }
]

https://mongoplayground.net/p/bYcLEzrF5QT

>Solution :

Add a match stage, to filter stages with the empty drawn tickets. Like this:

db.lotteryticket.aggregate([
  {
    $lookup: {
      from: "lotterydrawing",
      localField: "ticket",
      foreignField: "ticket",
      as: "drawnticket",
      
    }
  },
  {
    "$match": {
      $expr: {
        "$gt": [
          {
            $size: "$drawnticket"
          },
          0
        ]
      }
    }
  }
])

Playground.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading