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

MongoDB - Perbarui bidang dalam objek array berdasarkan nilai bidang array bersarang

Silakan coba ini :

db.Product.findOneAndUpdate(
        { _id: 123 },
        {
            $set: {
                'variations.$[item].valueList.$[nameField].value': 'newRed',
                'variations.$[item].picture': 'newURL' // item is each object in variations which is being checked in arrayFilters.
            }
        },
        {
            arrayFilters: [{ 'item.valueList.value': 'oldRed' }, { 'nameField.value': 'oldRed' }],
            new: true
        }
   )

Data Pengumpulan :

{
    "_id" : 123,
    "variations" : [ 
        {
            "id" : 1,
            "picture" : "https://example.picture.com",
            "valueList" : [ 
                {
                    "name" : "color",
                    "value" : "oldRed"
                }, 
                {
                    "name" : "size",
                    "value" : "M"
                }, 
                {
                    "name" : "color",
                    "value" : "oldRed"
                }
            ]
        }, 
        {
            "id" : 2,
            "picture" : "https://example.picture.com",
            "valueList" : [ 
                {
                    "name" : "color",
                    "value" : "black"
                }, 
                {
                    "name" : "size",
                    "value" : "M"
                }
            ]
        }, 
        {
            "id" : 3,
            "picture" : "https://example3.picture.com",
            "valueList" : [ 
                {
                    "name" : "color",
                    "value" : "oldRed"
                }, 
                {
                    "name" : "size",
                    "value" : "M"
                }
            ]
        }
    ]
}

Hasil :

/* 1 */
{
    "_id" : 123,
    "variations" : [ 
        {
            "id" : 1,
            "picture" : "newURL",
            "valueList" : [ 
                {
                    "name" : "color",
                    "value" : "newRed"
                }, 
                {
                    "name" : "size",
                    "value" : "M"
                }, 
                {
                    "name" : "color",
                    "value" : "newRed"
                }
            ]
        }, 
        {
            "id" : 2,
            "picture" : "https://example.picture.com",
            "valueList" : [ 
                {
                    "name" : "color",
                    "value" : "black"
                }, 
                {
                    "name" : "size",
                    "value" : "M"
                }
            ]
        }, 
        {
            "id" : 3,
            "picture" : "newURL",
            "valueList" : [ 
                {
                    "name" : "color",
                    "value" : "newRed"
                }, 
                {
                    "name" : "size",
                    "value" : "M"
                }
            ]
        }
    ]
}



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Kunci Komposit MongoDB

  2. Bagaimana cara mengonversi Dokumen MongoDB secara langsung ke Jackson JsonNode di Java

  3. Hitung jumlah objek bersarang dengan C#

  4. WAMP MongoDB - ekstensi PHP

  5. Bagaimana cara memulai kembali rantai janji secara kondisional dari awal?