Mongodb- အစုအဝေးတစ်ခုသို့ အကွက်အသစ်တစ်ခုထည့်နည်း
MongoDB ရှိ စုစည်းမှုတစ်ခုစီရှိ စာရွက်စာတမ်းတစ်ခုစီသို့ အကွက်အသစ်တစ်ခုထည့်ရန် အောက်ပါနည်းလမ်းများကို အသုံးပြုနိုင်သည်။
နည်းလမ်း 1- တန်ဖိုးများမပါဘဲ အကွက်အသစ်တစ်ခုကို ထည့်ပါ။
db.collection.updateMany({}, { $set :{" new_field ": null}})
နည်းလမ်း 2- သီးခြားတန်ဖိုးတစ်ခုဖြင့် အကွက်အသစ်တစ်ခုကို ထည့်ပါ။
db.collection.updateMany({}, { $set :{" new_field ": 10 }})
နည်းလမ်း 3- ရှိပြီးသားအကွက်များမှ တန်ဖိုးများကို အသုံးပြု၍ အကွက်အသစ်တစ်ခုထည့်ပါ။
db.collection.updateMany( {}, [ {" $set ": {" name ": { " $concat ": [" $field1 ", " ", " $field2 "]}}} ] )
အောက်ပါဥပမာများသည် အောက်ပါစာရွက်စာတမ်းများပါရှိသော စုစည်း အဖွဲ့ တစ်ခုနှင့် နည်းလမ်းတစ်ခုစီကို အသုံးပြုနည်းကို ပြသသည်-
db.teams.insertOne({team: " Mavs ", position: " Guard ", points: 31 }) db.teams.insertOne({team: " Spurs ", position: " Guard ", points: 22 }) db.teams.insertOne({team: " Rockets ", position: " Center ", points: 19 }) db.teams.insertOne({team: " Warriors ", position: " Forward ", points: 26 }) db.teams.insertOne({team: " Cavs ", position: " Guard ", points: 33 })
ဥပမာ 1- တန်ဖိုးများမပါဘဲ အကွက်အသစ်တစ်ခုကို ထည့်ပါ။
စုစည်းမှုတွင် ရှိပြီးသားစာရွက်စာတမ်းတစ်ခုစီအတွက် null value ဖြင့် “ bounces” ဟုခေါ်သော အကွက်အသစ်တစ်ခုကို ထည့်ရန် အောက်ပါကုဒ်ကို အသုံးပြုနိုင်ပါသည်။
db.teams.updateMany({}, { $set :{" rebounds ": null}})
ပထမမွမ်းမံထားသောစာရွက်စာတမ်းများကိုပြသရန် အောက်ပါမေးခွန်းကို ကျွန်ုပ်တို့အသုံးပြုနိုင်သည်-
db.teams.find().limit( 3 )
ဤမေးမြန်းချက်သည် အောက်ပါစာရွက်စာတမ်းများကို ပြန်ပေးသည်-
{ _id: ObjectId("6189325896cd2ba58ce928e5"), team: 'Mavs', position: 'Guard', points: 31, rebounds: null } { _id: ObjectId("6189325896cd2ba58ce928e6"), team: 'Spurs', position: 'Guard', points: 22, rebounds: null } { _id: ObjectId("6189325896cd2ba58ce928e7"), team: 'Rockets', position: 'Center', points: 19, rebounds: null }
စာရွက်စာတမ်းတစ်ခုစီတွင် null value ဖြင့် “ bounces” ဟုခေါ်သော အကွက်တစ်ခု ရှိနေကြောင်း သတိပြုပါ။
ဥပမာ 2- သီးခြားတန်ဖိုးတစ်ခုဖြင့် အကွက်အသစ်တစ်ခုကို ထည့်ပါ။
စုစည်းမှုရှိ ရှိပြီးသားစာရွက်စာတမ်းတစ်ခုစီသို့ 10 တန်ဖိုးရှိသော “ bounces” ဟုခေါ်သော အကွက်အသစ်တစ်ခုကို ထည့်ရန် အောက်ပါကုဒ်ကို အသုံးပြုနိုင်ပါသည် ။
db.teams.updateMany({}, { $set :{" rebounds ": 10 }})
ပထမမွမ်းမံထားသောစာရွက်စာတမ်းများကိုပြသရန် အောက်ပါမေးခွန်းကို ကျွန်ုပ်တို့အသုံးပြုနိုင်သည်-
db.teams.find().limit( 3 )
ဤမေးမြန်းချက်သည် အောက်ပါစာရွက်စာတမ်းများကို ပြန်ပေးသည်-
{ _id: ObjectId("6189325896cd2ba58ce928e5"), team: 'Mavs', position: 'Guard', points: 31, rebounds: 10 } { _id: ObjectId("6189325896cd2ba58ce928e6"), team: 'Spurs', position: 'Guard', points: 22, rebounds: 10 } { _id: ObjectId("6189325896cd2ba58ce928e7"), team: 'Rockets', position: 'Center', points: 19, rebounds: 10 }
စာရွက်စာတမ်းတစ်ခုစီတွင် တန်ဖိုး 10 ရှိသော “ bounces” ဟုခေါ်သော အကွက်တစ်ခု ရှိသည်ကို သတိပြုပါ။
ဥပမာ 3- ရှိပြီးသားအကွက်များမှ တန်ဖိုးများကို အသုံးပြု၍ အကွက်အသစ်တစ်ခုထည့်ပါ။
တန်ဖိုးသည် ရှိပြီးသား “ အဖွဲ့” နှင့် “ ရာထူး” အကွက်များ ပေါင်းစပ်ထားသည့် “ အမည်” ဟုခေါ်သော အကွက်တစ်ခုကို ထည့်ရန် အောက်ပါကုဒ်ကို အသုံးပြုနိုင်ပါသည်။
db.teams.updateMany( {}, [ {" $set ": {" name ": { " $concat ": [" $team ", " ", " $position "]}}} ] )
ပထမမွမ်းမံထားသောစာရွက်စာတမ်းများကိုပြသရန် အောက်ပါမေးခွန်းကို ကျွန်ုပ်တို့အသုံးပြုနိုင်သည်-
db.teams.find().limit( 3 )
ဤမေးမြန်းချက်သည် အောက်ပါစာရွက်စာတမ်းများကို ပြန်ပေးသည်-
{ _id: ObjectId("618934cb96cd2ba58ce928ea"), team: 'Mavs', position: 'Guard', points: 31, name: 'Mavs Guard' } { _id: ObjectId("618934cb96cd2ba58ce928eb"), team: 'Spurs', position: 'Guard', points: 22, name: 'Spurs Guard' } { _id: ObjectId("618934cb96cd2ba58ce928ec"), team: 'Rockets', position: 'Center', points: 19, name: 'Rockets Center' }
စာရွက်စာတမ်းတစ်ခုစီတွင် ယခုတန်ဖိုးသည် “ အသင်း” နှင့် “ ရာထူး” အကွက်များပေါင်းစပ်ထားသော “ အမည်” ဟုခေါ်သော အကွက်တစ်ခု ရှိနေကြောင်း သတိပြုပါ။
မှတ်ချက် – updateMany() လုပ်ဆောင်ချက်အတွက် စာရွက်စာတမ်းအပြည့်အစုံကို ဤနေရာတွင် ရှာဖွေနိုင်ပါသည်။
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် MongoDB တွင် အခြားဘုံအလုပ်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-
MongoDB- အကွက်များကို အမည်ပြောင်းနည်း
MongoDB- အကွက်များကို ဖျက်နည်း
MongoDB- အကွက်တွင် စာကြောင်းပါရှိမရှိ စစ်ဆေးနည်း
MongoDB- ရက်စွဲအပိုင်းအခြားတစ်ခုဖြင့် မေးမြန်းနည်း
MongoDB- “ Like” ပုံမှန်အသုံးအနှုန်းဖြင့်မေးမြန်းနည်း