個人用の簡易メモです。
起動
シェル
mongo
コマンド
ヘルプ
help
DBの切り替え
use <db_name>
CRUD
Create
db.mycoll.insert(<document>)
Read
db.mycoll.find(<query>, <projection>)
query - 条件
projection - 取得したいフィールド
{field1: value1, field2: value2} - AND条件の検索
演算子 | 意味 |
---|---|
$lt | より小さい |
$lte | 以下 |
$gt | より大きい |
$gte | 以上 |
$ne | 等しくない |
$exists | 存在有無 |
$or | OR条件 |
db.mycoll.find({filed1: {$ne: 'val1'}, fileld2: {$gte: 100})
db.mycoll.find({field1: {$exists: false}})
db.mycoll.find({field1: 'val1', $or: [{field2: 'val2'}, {field2: 'val3'}, {ield3: 'val4'}]})
Update
db.mycoll.update(<query>, <object>, <options>)
query - 条件
object - 設定値
options - upsert, multi
条件にマッチしたドキュメントを置き換える。
更新修飾子 | 意味 |
---|---|
$set | フィールドの一部を更新 |
$inc | フィールドの値を増やす |
$push | フィールドに値を追加 |
db.mycoll.update({field1: 'val1'}, {$set: {field2: 'val2'}})
db.mycoll.update({field1: 'val1'}, {$inc: {field2: -2}})
db.mycoll.update({field1: 'val1'}, {$push: {field2: 'val2'}})
db.mycoll.update({field1: 'val1'}, {$inc {field2: 1}}, {upsert: true})
Delete
db.mycoll.remove(<query>, <justOne>)