1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Mongoでよく使うコマンド

Last updated at Posted at 2021-01-28

すぐ忘れちゃうのでメモ。一般的なsqlとの比較。

select文

and条件、 is not 、betweenとか

select 
  user_id 
from 
  tabel_name
where 
  hoge = 'aaa'
  and fuga <> 'bbb'
  and time between '2021-01-01' and '2021-01-31'
;


'user_id':11 は0以外ならなんでも良いらしい。
_id:0 はmongo独自で持ってるidを表示しないために。
tabel_nameはコレクション名に置き換わっています。

db.{コレクション名}.find(
    {
        'hoge': 'aaa'
        'fuga':{$ne:'bbb'},
        'time':{$gte:ISODate('2021-01-01T00:00:00Z')},
        'time':{$lte:ISODate('2021-01-31T00:00:00Z')}
    },
    { 'user_id':1, _id:0}
);

その他いろいろ

count

db.{コレクション名}.find({条件}, {対象フィールド: 1}).count();

limit

db.{コレクション名}.find({条件}, {対象フィールド: 1}).limit({数字});
1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?