MongoDB ドキュメント(レコード)のプロパティを削除するサンプル

環境
MongoDB 6.0.2

構文
db.コレクション名.updateOne({ 条件 }, { $unset:{ プロパティ })
ドキュメント( レコード)のプロパティを削除するには、「db.コレクション名.updateOne()」で
「$unset」を使用します。

使用例
「cft」というコレクション(テーブル)のドキュメント( レコード)のプロパティを削除します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
> db.cft.find()
{ "_id" : ObjectId("61ad8a5a6b99b585a9389a04"), "name" : "yamato", "age" : 10, "gender" : "m" }
> db.cft.find() { "_id" : ObjectId("61ad8a5a6b99b585a9389a04"), "name" : "yamato", "age" : 10, "gender" : "m" }
> db.cft.find()

{ "_id" : ObjectId("61ad8a5a6b99b585a9389a04"), "name" : "yamato", "age" : 10, "gender" : "m" }

プロパティ「name」が「yamato」のものの「age」を削除します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
> db.cft.updateOne( { name:'yamato' }, { $unset:{ age: "" } } )
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
> db.cft.updateOne( { name:'yamato' }, { $unset:{ age: "" } } ) { acknowledged: true, insertedId: null, matchedCount: 1, modifiedCount: 1, upsertedCount: 0 }
> db.cft.updateOne( { name:'yamato' }, { $unset:{ age: "" } } )
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0
}

 

MongoDB

Posted by arkgame