Mongodb Study Notes

Source: Internet
Author: User
Tags findone

Mongodb tool:

1. mongodump and mongorestore

Bson beifen

2. Export export and Export Import

Multiple formats are available everywhere

3. Sniff

Provides shell statements for Operations sent to the database

4. Reset stat

Provide mongo status statistics, allocated memory and server connections.

5. bsondump and Program Files

Open javascript shell

> Mongo

> Db. numbers. find (). explain (); // view the execution plan

> 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 16 MB, about store
24000 namespaces, that mean is totalof indexes and collections shoshould 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 256 mb

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

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.