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

Kueri agregasi MongoDb dengan $group dan $push into subdocument

Coba jalankan pipa agregasi berikut, kuncinya menggunakan $proyek pipa untuk membuat attendeeContainer subdokumen:

db.event.aggregate([
    { "$unwind": "$attendeeContainer.attendees" },
    {
        "$lookup" : { 
            "from" : "contactinfo", 
            "localField" : "attendeeContainer.attendees.contact",
            "foreignField" : "_id", 
            "as" : "attendeeContainer.attendees.contactInfo" 
        }
    },
    { "$unwind": "$attendeeContainer.attendees.contactInfo" },
    {
        "$group": {
            "_id" : "$_id",
            "name": { "$first": "$name" },   
            "min" : { "$first": "$attendeeContainer.min" },
            "max" : { "$first": "$attendeeContainer.max" },
            "attendees": { "$push": "$attendeeContainer.attendees" }            
        }
    },
    {
        "$project": {
            "name": 1,
            "attendeeContainer.min": "$min",
            "attendeeContainer.max": "$min",
            "attendeeContainer.attendees": "$attendees"
        }
    }
])

Kiat Debug

Men-debug pipa pada tahap ke-4, Anda akan mendapatkan hasilnya

db.event.aggregate([
    { "$unwind": "$attendeeContainer.attendees" },
    {
        "$lookup" : { 
            "from" : "contactinfo", 
            "localField" : "attendeeContainer.attendees.contact",
            "foreignField" : "_id", 
            "as" : "attendeeContainer.attendees.contactInfo" 
        }
    },
    { "$unwind": "$attendeeContainer.attendees.contactInfo" },
    {
        "$group": {
            "_id": "$_id",
            "name": { "$first": "$name" },   
            "min" : { "$first": "$attendeeContainer.min" },
            "max" : { "$first": "$attendeeContainer.max" },
            "attendees": { "$push": "$attendeeContainer.attendees" }            
        }
    }/*,
    {
        "$project": {
            "name": 1,
            "attendeeContainer.min": "$min",
            "attendeeContainer.max": "$min",
            "attendeeContainer.attendees": "$attendees"
        }
    }*/
])

Hasil saluran

{
    "_id" : ObjectId("582c789282a9183adc0b53f5"),
    "name" : "Some Event",
    "min" : 0,
    "max" : 10,
    "attendees" : [ 
        {
            "type" : 1,
            "status" : 2,
            "contact" : ObjectId("582c787682a9183adc0b53f3"),
            "contactInfo" : {
                "_id" : ObjectId("582c787682a9183adc0b53f3"),
                "name" : "John Doe",
                "age" : 35
            }
        }, 
        {
            "type" : 7,
            "status" : 4,
            "contact" : ObjectId("582c787682a9183adc0b53f4"),
            "contactInfo" : {
                "_id" : ObjectId("582c787682a9183adc0b53f4"),
                "name" : "Peter Pan",
                "age" : 60
            }
        }
    ]
}

dan $project pipeline akan memberi Anda hasil yang diinginkan:

db.event.aggregate([
    { "$unwind": "$attendeeContainer.attendees" },
    {
        "$lookup" : { 
            "from" : "contactinfo", 
            "localField" : "attendeeContainer.attendees.contact",
            "foreignField" : "_id", 
            "as" : "attendeeContainer.attendees.contactInfo" 
        }
    },
    { "$unwind": "$attendeeContainer.attendees.contactInfo" },
    {
        "$group": {
            "_id": "$_id",
            "name": { "$first": "$name" },   
            "min" : { "$first": "$attendeeContainer.min" },
            "max" : { "$first": "$attendeeContainer.max" },
            "attendees": { "$push": "$attendeeContainer.attendees" }            
        }
    },
    {
        "$project": {
            "name": 1,
            "attendeeContainer.min": "$min",
            "attendeeContainer.max": "$min",
            "attendeeContainer.attendees": "$attendees"
        }
    }/**/
])

Keluaran yang Diinginkan/Aktual

{
    "_id" : ObjectId("582c789282a9183adc0b53f5"),
    "name" : "Some Event",
    "attendeeContainer" : {
        "min" : 0,
        "max" : 10,
        "attendees" : [ 
            {
                "type" : 1,
                "status" : 2,
                "contact" : ObjectId("582c787682a9183adc0b53f3"),
                "contactInfo" : {
                    "_id" : ObjectId("582c787682a9183adc0b53f3"),
                    "name" : "John Doe",
                    "age" : 35
                }
            }, 
            {
                "type" : 7,
                "status" : 4,
                "contact" : ObjectId("582c787682a9183adc0b53f4"),
                "contactInfo" : {
                    "_id" : ObjectId("582c787682a9183adc0b53f4"),
                    "name" : "Peter Pan",
                    "age" : 60
                }
            }
        ]
    }
}



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Bagaimana cara memperbarui sejumlah besar dokumen di MongoDB dengan paling efisien?

  2. Filter agregasi setelah $lookup

  3. Mengindeks array paralel di Mongodb

  4. Mengimpor json dari file ke mongodb menggunakan mongoimport

  5. Mencoba menemukan dengan $regex beberapa bidang secara bersamaan di Mongodb