0% found this document useful (0 votes)
4 views24 pages

Day - 09 - Deep-Dive Into MongoDB - Update Operators

The document provides an overview of MongoDB update operators, detailing various types such as field and array operators. It includes examples of using operators like $set, $unset, $rename, $addToSet, $push, and $pop to manipulate data within collections. The document serves as a guide for understanding how to perform updates in MongoDB effectively.

Uploaded by

gatewinchester
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views24 pages

Day - 09 - Deep-Dive Into MongoDB - Update Operators

The document provides an overview of MongoDB update operators, detailing various types such as field and array operators. It includes examples of using operators like $set, $unset, $rename, $addToSet, $push, and $pop to manipulate data within collections. The document serves as a guide for understanding how to perform updates in MongoDB effectively.

Uploaded by

gatewinchester
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Deep-Dive into

MongoDB:
Update Operators

နဲ ဆိ
့ ုင်တာေတွကို ေဆွးေ းွ ကမယ်။
Contents

01 02

Update Indexes
Operators

အသံုးများ တဲ ့ update operators Indexes ေတွရဲ့


ေတွကို ေလ့လာ ကမယ်။ အလုပ်လပ ု ် ပံုအေ ကာင်း
ေလ့လာ ကမယ်။
Operators - Update
Type of Update Operators - update()

● Field
● Array
// The updateOne()
// မှ ာ သံုးတဲ့ syntax ေတွက -

db.collection.updateOne(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ],
hint: <document|string> // Available starting in MongoDB 4.2.1
}
)
Field Operators
$set - field operator
// သံုးချင်တဲ့ db ကို ေရွ းမယ်။
use alexsnowschool

// products collection မှ ာ data အရင်ထည့်မယ်။


db.products.insertOne(
{
_id: 100,
quantity: 250,
instock: true,
reorder: false,
details: { model: "14QQ", make: "Clothes Corp" },
tags: [ "apparel", "clothing" ],
ratings: [ { by: "Customer007", rating: 4 } ]
}
)
Field Operators - $set

// top level fields ြဖစ် တဲ့ quantity,


// details နဲ ့ tags တို့ ရဲ့ value ကို
// $set operator ကို သံုး ပီ း update လုပ်မယ်။
db.products.updateOne(
{ _id: 100 },
{ $set:
{
quantity: 500,
details: { model: "2600", make: "Fashionaires" },
tags: [ "coats", "outerwear", "clothing" ]
}
}
)
Field Operators - $set

// Embedded Documents ြဖစ် တဲ့ details ကို update လုပ်မယ်၊၊


db.products.updateOne(
{ _id: 100 },
{ $set: { "details.make": "Kustom Kidz" } }
)
// ratings Arrays မှ ာ ပါတဲ့ rating ကို ေြပာင်းမယ်။
db.products.updateOne(
{ _id: 100 },
{ $set:
{
"tags.1": "rain gear",
"ratings.0.rating": 2
}
}
)
$unset - field operator
Field Operators - $unset

// products collection မှ ာ data အသစ် ထည့်မယ်။


db.products.insertMany( [
{ "item": "chisel", "sku": "C001", "quantity": 4, "instock": true },
{ "item": "hammer", "sku": "unknown", "quantity": 3, "instock": true },
{ "item": "nails", "sku": "unknown", "quantity": 100, "instock": true }
] )

// $unset operator ကို သံုး ပီ း quantity နဲ ့ instock fields ေတွကို ြဖုတ် မယ်။
db.products.updateOne(
{ sku: "unknown" },
{ $unset: { quantity: "", instock: "" } }
)
$rename - field operator
db.students.insertMany( [
{
"_id": 1,
"alias": [ "The American Cincinnatus", "The American Fabius" ],
"mobile": "555-555-5555",
"nmae": { "first" : "george", "last" : "washington" }
},
{
"_id": 2,
"alias": [ "My dearest friend" ],
"mobile": "222-222-2222",
"nmae": { "first" : "abigail", "last" : "adams" }
},
{
"_id": 3,
"alias": [ "Amazing grace" ],
"mobile": "111-111-1111",
"nmae": { "first" : "grace", "last" : "hopper" }
}
] )
Field Operators - $rename

// nmae ကို name ဆို ပီ း feild name ေြပာင်းမယ်။


db.students.updateMany( {}, { $rename: { "nmae": "name" } } )

// embedded document မှ ာ ရှ ိ တဲ့ first ကို fname လို့ ေြပာင်းမယ်။


db.students.updateOne( { _id: 1 }, { $rename: { "name.first": "name.fname"
} } )

// အရင်က မရှ ိ တဲ့ field - wife ကို spouse လို့ ေြပာင်း ကည့်မယ်။
db.students.updateOne( { _id: 1 }, { $rename: { 'wife': 'spouse' } } )
Array Operators
$addToSet - array operator
db.inventory.insertOne(
{ _id: 1, item: "polarizing_filter",
tags: [ "electronics", "camera" ] }
)
// Add to Array
db.inventory.updateOne(
{ _id: 1 },
{ $addToSet: { tags: "accessories" } }
)
// Value Already Exists
db.inventory.updateOne(
{ _id: 1 },
{ $addToSet: { tags: "camera" } }
)
// $each Modifier
db.inventory.updateOne(
{ _id: 2 },
{ $addToSet: { tags: { $each: [ "camera", "electronics", "accessories" ] } } }
)
$push - array operator
// Append a Value to an Array
db.students.insertOne( { _id: 4, scores: [ 44, 78, 38, 80 ] } )
db.students.updateOne(
{ _id: 4 },
{ $push: { scores: 89 } }
)

// Append a Value to Arrays in Multiple Documents


db.students.insertMany( [
{ _id: 5, scores: [ 45, 78, 38, 80, 89 ] } ,
{ _id: 6, scores: [ 46, 78, 38, 80, 89 ] } ,
{ _id: 7, scores: [ 47, 78, 38, 80, 89 ] }
] )
db.students.updateMany(
{ },
{ $push: { scores: 95 } }
)
// Use $push Operator with Multiple Modifiers
db.students.insertOne(
{
"_id" : 8,
"quizzes" : [
{ "wk": 1, "score" : 10 },{ "wk": 2, "score" : 8 },
{ "wk": 3, "score" : 5 },{ "wk": 4, "score" : 6 }
]
}
)
db.students.updateOne(
{ _id: 8 },
{
$push: {
quizzes: {
$each: [ { wk: 5, score: 8 }, { wk: 6, score: 7 }, { wk: 7, score: 6 } ],
$sort: { score: -1 },
$slice: 3
}
}
}
)
$pop - array operator
// Remove the First Item of an Array
db.students.insertOne( { _id: 9, scores: [ 8, 9, 10 ] } )
db.students.updateOne( { _id: 9 }, { $pop: { scores: -1 } } )

// Remove the Last Item of an Array


db.students.insertOne( { _id: 10, scores: [ 9, 10 ] } )
db.students.updateOne( { _id: 10 }, { $pop: { scores: 1 } } )
Thank you!
တစ် ရက်နည်းနည်းစီ တြဖည်းြဖည်း
Alex Snow School
အတူတေ
ူ လ့လာ ကစို့ ။

alexsnowschool.org/

@alexsnowschool
facebook.com/groups/alexsnowschool [email protected]

github.com/alexsnowschool

You might also like