mongodb 學習筆記

來源:互聯網
上載者:User

Mongodb 內建工具:

1. mongodump 和mongorestore

     提供熱備份 原生態bson beifen

2.mongoexport 和 mongoimport

     可以到處多種格式

3.mongosniff

    提供發送到資料庫的操作的shell 語句

4.mongostat

     提供mongo狀態的統計資訊,分配的記憶體和伺服器串連數。

5.bsondump 和 mongofiles

開啟javascript shell

>mongo

>db.numbers.find().explain();///查看執行計畫

>db.numbers.ensureIndex({number:1});//create index on specia column

>db.numbers.getIndexes();//view all indexes

>db.stats();// view db infomation

>db.runCommand({dbstats:1});

>db.numbers.stats();//view collectoin information

>db.runCommand({collstats:'numbers'});

> db.runCommand;
function (obj) {
    if (typeof obj == "string") {
        var n = {};
        n[obj] = 1;
        obj = n;
    }
    return this.getCollection("$cmd").findOne(obj);

equals the below functional:

> db.$cmd.findOne({collstats:"numbers"});
{
        "ns" : "config.numbers",
        "count" : 2000000,
        "size" : 80000040, //
        "avgObjSize" : 40.00002,
        "storageSize" : 121724928, ///collection extent total size
        "numExtents" : 13,
        "nindexes" : 2,
        "lastExtentSize" : 34312192,
        "paddingFactor" : 1,
        "systemFlags" : 1,
        "userFlags" : 0,
        "totalIndexSize" : 115232544,
        "indexSizes" : {
                "_id_" : 64909264,
                "number_1" : 50323280
        },
        "ok" : 1
}

filesize:file total size .
storagesize:collection total size
datasize:bson size
>

>db.createCollection("garden");
>db.createCollection("garden",{size:200000}); //byte size

>db.garden.renameCollection("namegarden");

mongodb.lock is store server process id ,the file name according the db name,garden.ns is first file,extend name is ns which mean is namespaces,every collection or index has ownerd itself's namespace ,on default situation,.ns file size fixed on 16Mb,about store
24000 namespaces,that mean is totalof indexes and collections should not more than 24000,if have nessary use command --nssize to extend its size.

The data file name start with 0 as end file name of spacefiles.
The second file size is double more than the first file . for exameple :garden.0 is 128 mb,garden.1 is 256mb

garden.2 is 512 mb and reach on the max size 2Gb.

bason date store date data,use 64 bit integer maked unix epoch millesecond ,use UTC (coodinnated universal time ) start at 1970--01-01

> doc ={ "_id" : ObjectId("51c160540e0e52d6d7fec634"), "msg" : "log info ", "sequenceNo" : 19 }
{
        "_id" : ObjectId("51c160540e0e52d6d7fec634"),
        "msg" : "log info ",
        "sequenceNo" : 19
}
> Object.bsonsize(doc);

mongodb $all $in use index ,$nin not use index,

$ne,$not use on regex ,$or ,$and $exists 

$elemMatch is a useful function,match much element on select collection

db.user.find({address:{$elemMatch:{name:"tom",state:"NY"}}});

$size The size of array which special collection selectd.

$where query special data where can't use simple sql to implement.

db.user.find({$where:"this.number > 1"});

$mod

db.number2.find({number:{$mod:[3,0]}});

_id :has two types for this primary key,2 and 7 ,2 is varchar,7 is object id.

$slice :sort precount or endcount 

$slice:12,$slice :-5

$slice:[20,10]

db.user.distinct("tags");

usage command;

findAndMofify:

> db.number2.findAndModify({query:{number:{$gt:50}},update:{$set:{number:100}}});
{
        "_id" : ObjectId("51c259f10e0e52d6d7fec63a"),
        "name" : "tom2",
        "number" : 60
}
> db.number2.find({number:{$gt:50}});
{ "_id" : ObjectId("51c259f10e0e52d6d7fec63a"), "name" : "tom2", "number" : 100 }

db.numbers2.update({},{name:"tom",$addToSet:{"name":"tom","cell phone number":"1232345"}});

db.numbers2.update({number:{$gt:100}},{$inc:{number:-1000000}});

db.number2.update({number:123},{$rename:{"name":"myname"}})

$unset is set null value,delete data compeltely use $pull and $pop

update atomic

db.numbers2.update({$atomic:true},{$set:{rating:0},false,false});

db.numbers2.remove({_id:123,{$atomic:true}});

> spec={ns:"config.numbers2",key:{"number":1},name:"zip"};
{ "ns" : "config.numbers2", "key" : { "number" : 1 }, "name" : "zip" }
> db.system.indexes.insert(spec,true);
> > spec={ns:"config.numbers2",key:{"number":1},name:"zip"};
Thu Jun 20 00:02:22 SyntaxError: syntax error (shell):1
> { "ns" : "config.numbers2", "key" : { "number" : 1 }, "name" : "zip" }
Thu Jun 20 00:02:22 SyntaxError: invalid label (shell):1
> > db.system.indexes.insert(spec,true);
Thu Jun 20 00:02:22 SyntaxError: syntax error (shell):1
> db.system.indexes.find();
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "config.test", "name" : "_id_" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "config.tenants", "name" : "_id_" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "config.agilysys", "name" : "_id_" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "config.users", "name" : "_id_" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "config.clients", "name" : "_id_" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "config.numbers2", "name" : "_id_" }
{ "v" : 1, "key" : { "number" : 1 }, "ns" : "config.numbers2", "name" : "number_1" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "config.logs", "name" : "_id_" }
{ "v" : 1, "key" : { "author" : 1 }, "ns" : "config.logs", "name" : "author_1" }
{ "v" : 1, "key" : { "actions.author" : 1 }, "ns" : "config.logs", "name" : "actions.author_1" }
{ "v" : 1, "key" : { "actions.author" : 1, "name" : "myIndex" }, "ns" : "config.logs", "name" : "actions.author_1_name_myIndex" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "config.number2", "name" : "_id_" }
>> db.system.indexes.dropIndex("actions.author_1_name_myIndex");
{ "errmsg" : "index not found", "ok" : 0 }
>> db.currentOp()   //view current DB log
{ "inprog" : [ ] }
>db.values.reIndex();

db.number2s.find().explain(true); //display all explain plans

db.number2s.find().hint({close:1}).explain();//force to use close as index condition

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.