sql >> Database >  >> NoSQL >> MongoDB

Mongodb verzamelt drie collecties

U kunt onderstaande aggregatiequery proberen.

De onderstaande query pusht de sections naar department wanneer ze lid worden en $group om de afdeling te pushen om de definitieve structuur te creëren.

db.college.aggregate([
  {
    "$match": {
      "university_id": "5834ecf7432d92675bde9d82"
    }
  },
  {
    "$lookup": {
      "localField": "_id",
      "from": "departments",
      "foreignField": "college_id",
      "as": "departments"
    }
  },
  {
   "$unwind": {
     "path": "$departments",
     "preserveNullAndEmptyArrays": true
    }
  },
  {
    "$lookup": {
      "localField": "departments._id",
      "from": "sections",
      "foreignField": "department_id",
      "as": "departments.sections"
    }
  },
  {
    "$group": {
      "_id": "$_id",
      "name": {
        "$first": "$name"
      },
      "university_id": {
        "$first": "$university_id"
      },
      "departments": {
        "$push": "$departments"
      }
    }
  }
])



  1. Bereikquery's in Redis - Spring Data Redis

  2. MongoDB op de zwarte lijst gezet Fatale fout

  3. Referentiesets implementeren in Redis

  4. technieken voor het opslaan van bibliotheken in mongoDB's system.js