mongodbコマンド使用大全(共通コマンド)

16477 ワード

mongodbコマンド使用大全(共通コマンド)データベース共通コマンド
1、Help表示コマンドプロンプト
help
db.help();
db.yourColl.help();
db.youColl.find().help();
rs.help();
2、データベースの切り替え/作成
use yourDB; コレクションを作成すると、現在のデータベースが自動的に作成されます.
3、すべてのデータベースを問い合わせる
show dbs;
4、現在使用しているデータベースを削除する
db.dropDatabase();
5、指定したホストからデータベースをクローンする
db.cloneDatabase(“127.0.0.1”); 指定したマシン上のデータベースのデータを現在のデータベースにクローン化
6、指定したマシンから指定したデータベースデータをあるデータベースにコピーする
db.copyDatabase(“mydb”, “temp”, “127.0.0.1”);ネイティブmydbのデータをtempデータベースにコピー
7.現在のデータベースの修復
db.repairDatabase();
8、現在使用しているデータベースの表示
db.getName();
db; dbとgetNameメソッドは同じ効果で、現在使用されているデータベースをクエリーできます.
9、現在のdb状態を表示する
db.stats();
10、現在のdbバージョン
db.version();
11、現在のdbのリンクマシンアドレスを表示する
db.getMongo();
Collectionコレクション
1、集計セットの作成(table)
db.createCollection(“collName”, {size: 20, capped: 5, max: 100});
2、指定された名称の集計集合(table)を得る
db.getCollection(“account”);
3、現在のdbのすべての集約集合を得る
db.getCollectionNames();
4、現在のdbのすべての集計インデックスの状態を表示する
db.printCollectionStats();
ユーザー関連
1、ユーザーを追加する
db.addUser(“name”);
db.addUser(“userName”, “pwd123”, true); ユーザーの追加、パスワードの設定、読み取り専用かどうか
2、データベース認証、セキュリティモード
db.auth(“userName”, “123123”);
3、現在のすべてのユーザーを表示する
show users;
4、ユーザーの削除
db.removeUser(“userName”);
その他1、クエリ前のエラー情報db.getPrevError(); 2、エラーレコードdbをクリアする.resetError();
集計セットの基本情報1を表示し、ヘルプdbを表示する.yourColl.help(); 2.現在の集合を問い合わせるデータの数db.yourColl.count(); 3、データ空間の大きさdbを表示する.userInfo.dataSize(); 4、現在の集約集合が存在するdb dbを得る.userInfo.getDB(); 5.現在集積する状態dbを得る.userInfo.stats(); 6.集積集合の総サイズdbを得る.userInfo.totalSize(); 7、集積集合の貯蔵空間の大きさdb.userInfo.storageSize(); 8、Shardバージョン情報db.userInfo.getShardVersion()9、集約セット名db.userInfo.renameCollection(“users”); userInfoの名前をusers 10に変更し、現在の集約セットdbを削除する.userInfo.drop(); 集約コレクションクエリー
1.すべてのレコードdbを問い合わせる.userInfo.find(); 相当于:select*from userInfo;デフォルトでは、各ページに20個のレコードが表示されます.表示されない場合は、it反復コマンドで次のページのデータをクエリーできます.注意:itコマンドを入力して「;」を付けることはできません.しかし、各ページの表示データのサイズをDBQueryで設定することができます.shellBatchSize= 50;これでページごとに50件の記録が表示されます.
2.消去後の現在の集約集合の中のある列の重複データdbを問い合わせる.userInfo.distinct(“name”); nameの同じデータをフィルタリングするのは、select distict name from userInfoに相当します.
3、age=22のレコードdbを照会する.userInfo.find({“age”: 22}); 相当:select*from userInfo where age=22;
4、照会age>22の記録db.userInfo.find({age: {$gt: 22}}); 相当:select*from userInfo where age>22;
5、age<22のレコードdbを照会する.userInfo.find({age: {$lt: 22}}); 相当:select*from userInfo where age<22;
6、クエリーage>=25のレコードdb.userInfo.find({age: {$gte: 25}}); 相当:select*from userInfo where age>=25;
7、age<=25のレコードdbを照会する.userInfo.find({age: {$lte: 25}});
8、照会age>=23、age<=26 db.userInfo.find({age: { gte:23, g t e : 23 , lte: 26}});
9.クエリーnameにmongoのデータdbが含まれている.userInfo.find({name:/mongo/}); //%select*from userInfo where name like'%mongo%'に相当します.
10、クエリーnameのmongoで始まるdb.userInfo.find({name:/^mongo/}); select * from userInfo where name like ‘mongo%’;
11、指定列name、ageデータdbを問い合わせる.userInfo.find({}, {name: 1, age: 1}); 相当:select name,age from userInfo;もちろんnameはtrueやfalseを用いてもよいが,tureを用いた場合の河name:1の効果は同様であり,falseを用いるとnameを除外し,name以外の列情報を表示する.
12、指定列name、ageデータを照会し、age>25 db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1}); 相当:select name,age from userInfo where age>25;
13、年齢順の昇順:db.userInfo.find().sort({age: 1}); 降順:db.userInfo.find().sort({age: -1});
14、クエリーname=zhangsan、age=22のデータdb.userInfo.find({name: ‘zhangsan’, age: 22}); select*from userInfo where name=‘zhangsan’and age=‘22’;
15.クエリーの前の5つのデータdb.userInfo.find().limit(5); 相当于:selecttop 5*from userInfo;
16.10件以降のデータdbを問い合わせる.userInfo.find().skip(10); 相当:select*from userInfo where id not in(selecttop 10*from userInfo);
17.5-10間のデータdbを問い合わせる.userInfo.find().limit(10).skip(5); ページングに使用できます.limitはpageSize、skipは何ページ目*pageSizeです.
18、orとdbをクエリーする.userInfo.find({$or: [{age: 22}, {age: 25}]}); 相当:select*from userInfo where age=22 or age=25;
19.第1のデータdbを問い合わせる.userInfo.findOne(); 相当于:selecttop 1*from userInfo;db.userInfo.find().limit(1);
20.ある結果セットを問い合わせるレコード数db.userInfo.find({age: {$gte: 25}}).count(); 相当:select count(*)from userInfo where age>=20;
21、ある列に従ってdbを並べ替える.userInfo.find({sex: {$exists: true}}).count(); 相当:select count(sex)from userInfo;索引
1、インデックスdbを作成する.userInfo.ensureIndex({name: 1}); db.userInfo.ensureIndex({name: 1, ts: -1});
2.現在の集約セットのすべてのインデックスdbを問い合わせる.userInfo.getIndexes();
3.総索引記録サイズdbを表示する.userInfo.totalIndexSize();
4、現在の集合のすべてのindex情報dbを読み出す.users.reIndex();
5、指定インデックスdbを削除する.users.dropIndex(“name_1”);
6、すべてのインデックスdbを削除する.users.dropIndexes(); コレクションデータの変更、追加、削除
1、追加
db.users.save({name: ‘zhangsan’, age: 25, sex: true});

追加されたデータのデータ列は、固定されていません.追加されたデータに基づいて
2、修正
db.users.update({age: 25}, {$set: {name: 'changeName'}}, false, true);update users set name = ‘changeName’ where age = 25;

db.users.update({name: 'Lisi'}, {$inc: {age: 50}}, false, true);update users set age = age + 50 where name = ‘Lisi’;

db.users.update({name: 'Lisi'}, {$inc: {age: 50}, $set: {name: 'hoho'}}, false, true);update users set age = age + 50, name = ‘hoho’ where name = ‘Lisi’;

3、削除
db.users.remove({age: 132});

4、照会修正削除
db.users.findAndModify({
query: {age: {$gte: 25}}, 
sort: {age: -1}, 
update: {$set: {name: 'a2'}, $inc: {age: 2}},
remove: true
});

db.runCommand({ findandmodify : "users", 
query: {age: {$gte: 25}}, 
sort: {age: -1}, 
update: {$set: {name: 'a2'}, $inc: {age: 2}},
remove: true
});

updateまたはremoveのいずれかが必須パラメータです.その他のパラメータはオプションです.
ステートメントブロックアクション
1、簡単Hello World print(「Hello World!」);この書き方はprint関数を呼び出し、「Hello World!」に直接書き込む.の効果は同じです.
2、一つのオブジェクトをjsonに変換する
tojson(new Object());
tojson(new Object('a'));

3、循環追加データ
for (var i = 0; i < 30; i++) { … db.users.save({name: “u_” + i, age: 22 + i, sex: i % 2}); … }; これにより30個のデータがループ追加する、同様に括弧の表記for(var i=0;i<30;i+)dbを省略することもできる.users.save({name: “u_” + i, age: 22 + i, sex: i % 2}); dbを使うとusers.find()クエリーの場合、複数のデータが表示されて1ページ表示できない場合は、itで次のページの情報を表示できます.
4、findカーソルクエリー
var cursor = db.users.find(); while (cursor.hasNext()) { printjson(cursor.next()); } これにより、すべてのusers情報がクエリーされ、var cursor=dbと同様に書くことができる.users.find(); while (cursor.hasNext()) { printjson(cursor.next); } 同様に{}番号を省略できます
5、forEach反復サイクル
db.users.find().forEach(printjson);

forEachでは、各反復のデータ情報を処理するために関数を渡す必要があります.
6.findカーソルを配列として扱う
var cursor = db.users.find();
cursor[4];

インデックスが4であるデータを取得する配列として処理できる以上、その長さ:cursorを得ることができる.length();あるいはcursor.count(); データをループで表示することもできます
for (var i = 0, len = c.length(); i < len; i++) printjson(c[i]);

7.findカーソルを配列に変換する
> var arr = db.users.find().toArray();
> printjson(arr[2]);
 toArray         

8、自分たちの検索結果をカスタマイズする
   age <= 28      age    
db.users.find({age: {$lte: 28}}, {age: 1}).forEach(printjson);
db.users.find({age: {$lte: 28}}, {age: true}).forEach(printjson);
  age  
db.users.find({age: {$lte: 28}}, {age: false}).forEach(printjson);

9、forEach伝達関数表示情報
db.things.find({x:4}).forEach(function(x) {print(tojson(x));});

使用可能なコマンド:
$ne:    
$gt:   
$lt:   
$gte:      
$lte:      

現在の日付:
new Date().toLocaleString();

当日の新規ユーザー数:
db.user_info.find({create_time:{$gt:1440000000, $lt:1440086400}, group_id:{$ne:'0'}}).count();

当日の新規グループ:
db.user_info.distinct('group_id', {create_time:{$gt:1440000000, $lt:1440086400}, group_id:{$ne:'0'}});

当日の新規グループ数:
db.user_info.distinct('group_id', {create_time:{$gt:1440000000, $lt:1440086400}, group_id:{$ne:'0'}}).length;

当日の最新データ:
db.online_data.find({update_time:{$gt:1440000000, $lt:1440086400}, player_num:{$gt:0}}).count();

当日最大オンライン人数:
db.online_data.find({update_time:{$gt:1440000000, $lt:1440086400}}).sort({player_num:-1}).limit(1);

最新のデータ:
db.online_data.find().sort({update_time:-1}).limit(1);