Ryusuke Fuda's Tech Blog

Softweare Enginier about Web, iOS, Android.

mongoDB基本コマンド

/*
    DB
*/
db.help();
db.stats();
show dbs;
use videodb;
db.dropDatabase(); //connect しているDBを削除

/*
    collection
*/
db.createCollection("videos");
show collections;
db.videos.drop(); //collectionの削除
db.videos.renameCollection("movies");

/*
    document
*/
db.videos.find();
db.videos.inseart({"title":"Bike","tag":"{'cool','mountain'}"});
//$setつけないとdocumentがすべて上書きされて、'sort':'new'だけになってしまう。
db.videos.update({"_id":ObjectId("51a8bdf44b75ea7cfd331e7f")},{$set:{'sort':'new'}});
db.videos.update({"_id":ObjectId("51966fc9e66501b29f13b47e")},{$set:{tag:['scubadiving','skydiving']}});//配列登録
db.videos.update({"title":"Skydiving"},{$set:{"bad":"5","view":"1000"}});//複数フィールド追加
db.videos.update({"like":"500"},{$rename:{"cate":"category"}},false,true);//フィールド名前変更
db.videos.update({"like":"500"},{$rename:{"like":"good"}},false,true);
db.videos.update({"title":"Skydiving"},{$set:{"timestamp":(new Date()).getTime()}});//日付追加
db.videos.remove({"userId":"test"});//{"userId":"test"}のあるドキュメントすべて一括削除


/*
 *  Query
 *
 */

//複数条件抽出
db.videos.find({$and: [{"videoId": "427e096b2af75b5d45000002"},{"userId": "527e096b2af75b5d45000002"}]});