sql >> データベース >  >> NoSQL >> MongoDB

MongoDB-ネストされた配列のフィールド値に基づいて配列オブジェクトのフィールドを更新します

    これを試してください:

    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
            }
       )
    

    収集データ:

    {
        "_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"
                    }
                ]
            }
        ]
    }
    

    結果:

    /* 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. 仮想属性によるMongoidクエリDB

    2. PythonとMongoDBの使用開始

    3. MongoDB:一括挿入(Bulk.insert)と複数挿入(insert([...]))

    4. 画像ファイルをマングーススキーマのバイナリデータに保存し、画像をhtml形式で表示する