MongoDB
 sql >> Teknologi Basis Data >  >> NoSQL >> MongoDB

Mongodb mengumpulkan tiga koleksi

Anda dapat mencoba kueri agregasi di bawah ini.

Kueri di bawah ini mendorong sections ke department ketika mereka bergabung dan $group untuk mendorong departemen untuk membuat struktur akhir.

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. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Bagaimana Cara Menginstal Driver PHP MongoDB untuk XAMPP di OSX?

  2. Driver Node Asli MongoDB:Jelaskan Rusak?

  3. Apa bidang __v di luwak

  4. Cara mendapatkan hasil perintah mongo ke file datar

  5. NotUniqueError:Mencoba menyimpan duplikat kunci unik