sql >> Database >  >> NoSQL >> MongoDB

Hoe geneste $ lookup zoeken in MongoDB?

$lookup 3.6 syntaxis stelt u in staat om aan geneste tabellen deel te nemen en $unwind om een ​​matrixveld uit de invoerdocumenten te deconstrueren om een ​​document voor elk element uit te voeren. Zoiets

position.aggregate([
  { "$lookup": {
    "from": "companies",
    "let": { "companyId": "$company_id" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": [ "$_id", "$$companyId" ] } } },
      { "$lookup": {
        "from": "industries",
        "let": { "industry_id": "$industry_id" },
        "pipeline": [
          { "$match": { "$expr": { "$eq": [ "$_id", "$$industry_id" ] } } }
        ],
        "as": "industry"
      }},
      { "$unwind": "$industry" }
    ],
    "as": "company"
  }},
  { "$unwind": "$company" }
])

Met de 3.4-versie

position.aggregate([
  { "$lookup": {
    "from": "companies",
    "localField": "company_id",
    "foreignField": "_id",
    "as": "companies"
  }},
  { "$unwind": "$companies" },
  { "$lookup": {
    "from": "industries",
    "localField": "companies.industry_id",
    "foreignField": "_id",
    "as": "companies.industry"
  }},
  { "$unwind": "$companies.industry" },
  { "$group": {
    "_id": "$_id",
    "companies": { "$push": "$companies" }
  }}
])



  1. Inleiding tot Spring Data MongoDB

  2. Het wijzigen van de Redis-poort in Docker Compose werkt niet

  3. Hoe te controleren op nul/nul in Lua cjson van Redis?

  4. Redis haalt alle waarde van de lijst op zonder iteratie en zonder popping