Day - 09 - Deep-Dive Into MongoDB - Update Operators
Day - 09 - Deep-Dive Into MongoDB - Update Operators
MongoDB:
Update Operators
နဲ ဆိ
့ ုင်တာေတွကို ေဆွးေ းွ ကမယ်။
Contents
01 02
Update Indexes
Operators
● 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
// $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
// အရင်က မရှ ိ တဲ့ 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 } }
)
alexsnowschool.org/
@alexsnowschool
facebook.com/groups/alexsnowschool [email protected]
github.com/alexsnowschool