-
-
Save jiripospisil/36f37a41f9b2c7fe74e841eb064be9d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% mongo kk.js | |
MongoDB shell version v4.0.17 | |
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb | |
Implicit session: session { "id" : UUID("e953d631-6d34-4c7b-9fb3-2d99545b61a6") } | |
MongoDB server version: 4.0.17 | |
BEFORE | |
{ | |
"_id" : ObjectId("5ec7e6bbce039a1491d73730"), | |
"name" : "l33t shirt", | |
"price" : "1200", | |
"quantity" : "60", | |
"options" : { | |
"type" : "radio", | |
"title" : "color", | |
"opts" : [ | |
{ | |
"value" : "red", | |
"price" : "-100", | |
"qty" : 30 | |
}, | |
{ | |
"value" : "blue", | |
"price" : "+200", | |
"qty" : 10 | |
}, | |
{ | |
"value" : "green", | |
"price" : "+300", | |
"qty" : 20 | |
} | |
] | |
} | |
} | |
AFTER | |
{ | |
"_id" : ObjectId("5ec7e6bbce039a1491d73730"), | |
"name" : "l33t shirt", | |
"price" : "1200", | |
"quantity" : "60", | |
"options" : { | |
"type" : "radio", | |
"title" : "color", | |
"opts" : [ | |
{ | |
"value" : "red", | |
"price" : "-100", | |
"qty" : 30 | |
}, | |
{ | |
"value" : "blue", | |
"price" : "+200", | |
"qty" : 9 | |
}, | |
{ | |
"value" : "green", | |
"price" : "+300", | |
"qty" : 20 | |
} | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.test.remove({}); | |
db.test.insert({ | |
name: "l33t shirt", | |
price: "1200", | |
quantity: "60", | |
options: { | |
type: "radio", | |
title: "color", | |
opts: [ | |
{ value: "red" , price: "-100" , qty: 30 }, | |
{ value: "blue" , price: "+200" , qty: 10 }, | |
{ value: "green" , price: "+300" , qty: 20 } | |
] | |
} | |
}); | |
print("BEFORE"); | |
printjson(db.test.findOne()); | |
db.test.updateOne({ "options.opts.value": "blue" }, { $inc: { "options.opts.$.qty": -1 } }); | |
print("AFTER"); | |
printjson(db.test.findOne()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment