1. ホーム
  2. Database

mongodbの更新操作の更新

2022-02-11 20:25:41
<パス

https://docs.mongodb.com/manual/tutorial/update-documents/index.html これはmongdbのAPIを献本したものです。

公式サイトにある3つの更新操作について、ここではメモとして解説しています

db.inventory.updateOne(
   { item: "paper" }
   {
     $set: { "size.uom": "cm", status: "P" },
     $currentDate: { lastModified: true }
   }
)

これは、更新操作のために項目値を一致させて、一度に1ホップのレコードを更新することを意味する

db.inventory.updateMany(
   { "qty": { $lt: 50 } }
   {
     $set: { "size.uom": "in", status: "P" }
     $currentDate: { lastModified: true }
   }
)

これは、複数のレコードを一度に更新することを指しています。 { "qty": { $lt: 50 } } これは、qtyというプロパティに50未満の値が設定されているデータを指します。

The mongodb conditional operators, "$lt", "$lte", "$gt", "$gte", "$ne" are all the comparison operators, "<", "$lte", "$gt", "$gt", "$gte", "$ne".
corresponding to "<", "<=", ">", ">=", ">=", "! =".
Atomic operators: "$and", "$or", "$nor".
There are two ways to do an or query: one is to use $in to query multiple values of a key, and the other is to use $or to complete multiple key values for any given value.
$in is equivalent to the in operation of the SQL statement.
$nin is not.
$not is extremely useful when used in conjunction with regular expressions to query which documents do not match a particular pattern.
$slice is the slicing equivalent of the array function, retrieving an array of documents and fetching a portion of the array. Limiting a large number of elements in a collection saves bandwidth.
Theoretically this can be done with the limit() and skip() functions, but there is nothing to do with arrays. $slice can specify two arguments.
The first parameter indicates the total number of elements to be returned. The second argument is optional. The first argument defines the offset, if used.
and the second parameter is a finite number. The second parameter can also specify a negative number.
The $mod fetch operation.
The $size operator allows filtering the result to match an array of the specified number of elements.
The $exists operator allows to return a specific object. Note: The current version of $exists is not indexable, so using it requires a full table scan.
The $type operator allows to match results based on BSON types.
db.inventory.replaceOne(
   { item: "paper" },
   { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] }
)

db.inventory.replaceOne(
   { item: "paper" },
   { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] }
)

これは、item 属性の後にある、同じ項目値を持つすべてのデータを instock 属性に置き換えます。