Search This Blog

Sunday, April 1, 2018

update on mongo nexted array

Initial state of collection
    Query:
        db.nx.find({})
    confirm Result:
        {
            "_id" : ObjectId("5ab290094d5d23c1ed27c8a2"),
            "user" : "john",
            "cart_item" : [
            {
                "product" : "product1",
                "unit_price" : 5.0,
                "qty" : 10.0,
                "total" : 0.0
            },
            {
                "product" : "product2",
                "unit_price" : 6.0,
                "qty" : 15.0,
                "total" : 0.0
            },
            {
                "product" : "product3",
                "unit_price" : 4.0,
                "qty" : 30.0,
                "total" : 0.0
            }
            ]
        }


Updating product1 quantity by 5 more items

    db.nx.update( {"_id" : ObjectId("5ab290094d5d23c1ed27c8a2"), "cart_item.product" : "product1" } ,
                {$inc : {"cart_item.$.qty" : 5} } ,
                false ,
                true);
   
confirm Result:
    db.nx.find({})
Output:
    {
        "_id" : ObjectId("5ab290094d5d23c1ed27c8a2"),
        "user" : "john",
        "cart_item" : [
        {
            "product" : "product1",
            "unit_price" : 5.0,
            "qty" : 15.0,
            "total" : 0.0
        },
        {
            "product" : "product2",
            "unit_price" : 6.0,
            "qty" : 15.0,
            "total" : 0.0
        },
        {
            "product" : "product3",
            "unit_price" : 4.0,
            "qty" : 30.0,
            "total" : 0.0
        }
        ]
    }

setting product2 quantity to 20

Query :
    db.nx.update( {"_id" : ObjectId("5ab290094d5d23c1ed27c8a2"), "cart_item.product" : "product2" } ,
                {$set : {"cart_item.$.qty" : 20} } ,
                false ,
                true);
confirm Result:
    db.nx.find({})
Output:
    {
        "_id" : ObjectId("5ab290094d5d23c1ed27c8a2"),
        "user" : "john",
        "cart_item" : [
        {
            "product" : "product1",
            "unit_price" : 5.0,
            "qty" : 15.0,
            "total" : 0.0
        },
        {
            "product" : "product2",
            "unit_price" : 6.0,
            "qty" : 20.0,
            "total" : 0.0
        },
        {
            "product" : "product3",
            "unit_price" : 4.0,
            "qty" : 30.0,
            "total" : 0.0
        }
        ]
    }

updating quantity of each product to 25

Query:
    db.nx.update({"_id" : ObjectId("5ab290094d5d23c1ed27c8a2")},{$set:{"cart_item.$[].qty":25}})

confirm Result:
    db.nx.find()
Output:
    {
        "_id" : ObjectId("5ab290094d5d23c1ed27c8a2"),
        "user" : "john",
        "cart_item" : [
        {
            "product" : "product1",
            "unit_price" : 5.0,
            "qty" : 25.0,
            "total" : 0.0
        },
        {
            "product" : "product2",
            "unit_price" : 6.0,
            "qty" : 25.0,
            "total" : 0.0
        },
        {
            "product" : "product3",
            "unit_price" : 4.0,
            "qty" : 25.0,
            "total" : 0.0
        }
        ]
    }

setting quantity of only product1 &  product2 to 33

Query:
    db.nx.update(
        {"_id" : ObjectId("5ab290094d5d23c1ed27c8a2")},
        {$set: {"cart_item.$[i].qty": 33}},
        {arrayFilters: [{"i.product": { $in: [ "product1", "product2" ] }}]}
    )

confirm Result:
    db.nx.find()
Output:
    {
        "_id" : ObjectId("5ab290094d5d23c1ed27c8a2"),
        "user" : "john",
        "cart_item" : [
         {
            "product" : "product1",
            "unit_price" : 5.0,
            "qty" : 33.0,
            "total" : 0.0
        },
        {
            "product" : "product2",
            "unit_price" : 6.0,
            "qty" : 33.0,
            "total" : 0.0
        },
        {
            "product" : "product3",
            "unit_price" : 4.0,
            "qty" : 25.0,
            "total" : 0.0
        }
        ]
    }

Now lets consider plain Array

db.mx.insert({
        student : "sangram"
    exam:"test001",
    subjects:["hindi","marathi","english","history"],
    marks:[80,67,86,87],
})

Initial State of Collection:
    db.mx.find({})

    Output:
    {
        "_id" : ObjectId("5ac0f9188ce39a4fe0594edb"),
        "student" : "sangram",
        "exam" : "test001",
        "subjects" : [
        "hindi",
        "marathi",
        "english",
        "history"
        ],
        "marks" : [
        80,
        67,
        86,
        87
        ]
    }


Incrementing Each Subject Marks by 5:

    db.mx.update(
        {"_id" : ObjectId("5ac0f9188ce39a4fe0594edb")},
        { $inc : { "marks.$[]" : 5 }}
    )

Confirm Result:
    Query:db.mx.find({})
    Output:
    {
        "_id" : ObjectId("5ac0f9188ce39a4fe0594edb"),
        "student" : "sangram",
        "exam" : "test001",
        "subjects" : [
        "hindi",
        "marathi",
        "english",
        "history"
        ],
        "marks" : [
        85.0,
        72.0,
        91.0,
        92.0
        ]
    }


increase marks of all subject which are now 85

db.mx.update(
        {"_id" : ObjectId("5ac0f9188ce39a4fe0594edb")},
        { $inc : { "marks.$[i]" : 5 }},
        {arrayFilters: [{i: 85}]}
    )


Confirm Result:
    Query:db.mx.find({})
    Output:
        {
            "_id" : ObjectId("5ac0f9188ce39a4fe0594edb"),
            "student" : "sangram",
            "exam" : "test001",
            "subjects" : [
            "hindi",
            "marathi",
            "english",
            "history"
            ],
            "marks" : [
            90.0,
            72.0,
            91.0,
            92.0
            ]
        }

 increase marks of all subject which are now 90 or 72

    db.mx.update(
            {"_id" : ObjectId("5ac0f9188ce39a4fe0594edb")},
            { $inc : { "marks.$[i]" : 5 }},
            {arrayFilters: [{$or: [{i: 90}, {i: 72}]}]}
        )

Confirm Result:
    Query:db.mx.find({})
    Output:
        {
            "_id" : ObjectId("5ac0f9188ce39a4fe0594edb"),
            "student" : "sangram",
            "exam" : "test001",
            "subjects" : [
            "hindi",
            "marathi",
            "english",
            "history"
            ],
            "marks" : [
            95.0,
            77.0,
            91.0,
            92.0
            ]
        }

update marks to 50 where its position in array in 3rd

db.mx.update(
        {"_id" : ObjectId("5ac0f9188ce39a4fe0594edb")},
        {$set : {"marks.2" : 50}}
    )
   
Confirm Result:
    Query:db.mx.find({})
    Output:
        {
            "_id" : ObjectId("5ac0f9188ce39a4fe0594edb"),
            "student" : "sangram",
            "exam" : "test001",
            "subjects" : [
            "hindi",
            "marathi",
            "english",
            "history"
            ],
            "marks" : [
            95.0,
            77.0,
            50.0,
            92.0
            ]
        }

No comments:

Post a Comment