Skip to content

Instantly share code, notes, and snippets.

@jiripospisil
Created May 22, 2020 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiripospisil/36f37a41f9b2c7fe74e841eb064be9d7 to your computer and use it in GitHub Desktop.
Save jiripospisil/36f37a41f9b2c7fe74e841eb064be9d7 to your computer and use it in GitHub Desktop.
% 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
}
]
}
}
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