RedHat MongoDB Study Notes

Source: Internet
Author: User
Tags mongodb commands
ArticleDirectory
    • I. Software Packages
I. Software Packages

A) mongodb-linux-i686-static-1.8.1.tar

Ii. Installation Steps

A) tar xzf mongodb-linux-i686-static-1.8.1.tar

B) Music mongodb-linux-i686-static-1.8.1/usr/local/MongoDB

C) mkdir-P/data/DB // default database storage path

D) chown 'id-U'/data/DB

E) CD/usr/local/MongoDB

F)./bin/mongod // start mongod

G)./bin/Mongo // go to the command line management page.

H) killall Mongo // stop mongod

Iii. management commands

1. Related to Super Users:

1. # enter the database Admin

Use Admin

2. # Add or modify user passwords

DB. adduser ('name', 'pwd ')

3. # view the user list

DB. system. Users. Find ()

4. # User Authentication

DB. Auth ('name', 'pwd ')

5. # delete a user

DB. removeuser ('name ')

6. # view all users

Show users

7. # view all databases

Show DBS

8. # view all collections

Show collections

9. # view the status of each collection

DB. printcollectionstats ()

10. # view master-slave replication status

DB. printreplicationinfo ()

11. # restore the database

DB. repairdatabase ()

12. # Set the record profiling, 0 = off 1 = slow 2 = all

DB. setprofilinglevel (1)

13. # view profiling

Show Profile

14. # copy a database

DB. copydatabase ('mail _ ADDR ', 'mail _ addr_tmp ')

15. # Delete collection

DB. mail_addr.drop ()

16. # Delete the current database

DB. dropdatabase ()

17. # insert a piece of data

DB. User. insert ({uid: 1, name: "sdgasd": Age: 12, createtime: "12:12:12 "})

18. Exit command line

Exit

19. Create a table set for the first time and exit

Q

2. add, delete, and modify

1. # store nested objects

DB. foo. save ({'name': 'xy', 'address': {'city': 'beijinging', 'post': 100096}, 'phone': [138,139]})

2. # store array objects

DB. user_addr.save ({'uid': 'yushunzhi @ sohu.com ', 'al': ['test-1@sohu.com', 'test-2@sohu.com ']})

3. # modify according to query conditions. If the query condition does not exist, insert multiple records.

DB. Foo. Update ({'yy': 5}, {'$ set': {'xx': 2 }}, upsert = true, multi = true)

4. # delete records with YY = 5

DB. Foo. Remove ({'yy': 5 })

5. # delete all records

DB. Foo. Remove ()

3. Index

1. # Add an index: 1 (ascending),-1 (descending)

2. DB. Foo. ensureindex ({firstname: 1, lastname: 1 },{ unique: true });

3. # index sub-objects

4. DB. user_addr.ensureindex ({'al. Em ': 1 })

5. # View index information

6. DB. Foo. getindexes ()

7. DB. Foo. getindexkeys ()

8. # delete an index based on the index name

9. DB. user_addr.dropindex ('al. em_1 ')

4. Query

1. # search for all

2. DB. Foo. Find ()

3. # search for a record

4. DB. Foo. findone ()

5. # search 10 records based on conditions

6. DB. Foo. Find ({'msg ': 'Hello 1'}). Limit (10)

7. # Sort sorting

8. DB. deliver_status.find ({'from': 'ixigua @ Sina.com '}). Sort ({'dt',-1 })

9. DB. deliver_status.find (). Sort ({'ct ':-1}). limit (1)

10. # Count operation

11. DB. user_addr.count ()

12. # perform the distinct operation to query specified columns and repeat them.

13. DB. Foo. Distinct ('msg ')

14. # "> =" Operation

15. DB. Foo. Find ({"timestamp": {"$ GTE": 2 }})

16. # search for sub-objects

17. DB. Foo. Find ({'address. City': 'beijing '})

5. Management

1. # view the collection data size

2. DB. deliver_status.datasize ()

3. # view colleciont status

4. DB. deliver_status.stats ()

5. # query the size of all indexes

6. DB. deliver_status.totalindexsize ()

5. Advanced queries: Advanced Query

Conditional Operators
$ GT:>
$ LT: <
$ GTE:> =
$ LTE: <=
$ Ne :! =, <>
$ In: In
$ Nin: not in
$ All: All
$ Not: Anti-match (version 1.3.3 and later)
Query data with name <> "Bruce" and age> = 18
DB. Users. Find ({Name: {$ ne: "Bruce"}, age: {$ GTE: 18 }});
Query the data of creation_date> '2017-01-01 'and creation_date <= '2017-12-31'
DB. Users. Find ({creation_date: {$ GT: new date (2010,0, 1), $ LTE: new date (2010,11, 31 )});
Query age in (20, 22, 24, 26) data
DB. Users. Find ({age: {$ in: [20, 22, 24, 26]});
Query the data of age modulo 10 or 0
DB. Users. Find ('this. Age % 10 = 0 ');
Or
DB. Users. Find ({age: {$ mod: [10, 0]});
Match All
DB. Users. Find ({favorite_number: {$ All: [6, 8]});
{Name: 'David ', age: 26, favorite_number: [6, 8, 9]}
{Name: 'David ', age: 26, favorite_number: [6, 7, 9]}
Query records that do not match name = B *
DB. Users. Find ({Name: {$ not:/^ B .*/}});
Query data of age modulo 10 not equal to 0
DB. Users. Find ({age :{$ not :{$ mod: [10, 0] }});
# Some returned Fields
Select the returned age and _ id fields (the _ id field will always be returned)
DB. Users. Find ({}, {age: 1 });
DB. Users. Find ({}, {age: 3 });
DB. Users. Find ({}, {age: true });
DB. Users. Find ({name: "Bruce"}, {age: 1 });
0 is false, not 0 is true
Select the returned age, address, and _ id Fields
DB. Users. Find ({name: "Bruce"}, {age: 1, address: 1 });
Exclude returned age, address, and _ id Fields
DB. Users. Find ({}, {age: 0, address: false });
DB. Users. Find ({name: "Bruce"}, {age: 0, address: false });
Array Element count judgment
For {Name: 'David', age: 26, favorite_number: [6, 7, 9]} records
Match dB. Users. Find ({favorite_number :{$ size: 3 }});
Does not match dB. Users. Find ({favorite_number: {$ size: 2 }});
$ Exists
Query all records with name fields
DB. Users. Find ({Name: {$ exists: true }});
Query records that do not contain phone Fields
DB. Users. Find ({PHONE: {$ exists: false }});
$ Type determines the field type
Query all name fields of the character type
DB. Users. Find ({Name: {$ type: 2 }});
Query that all age fields are integer
DB. Users. Find ({age: {$ type: 16 }});
For character fields, you can use regular expressions.
Query all records with letters B or B leading
DB. Users. Find ({Name:/^ B. */I });
$ Elemmatch (1.3.1 and later)
Matches an element in an array field.
Javascript query and $ where Query
Query the records of age> 18. The query below is the same
DB. Users. Find ({age: {$ GT: 18 }});
DB. Users. Find ({$ where: "This. age> 18 "});
DB. Users. Find ("This. age> 18 ");
F = function () {return this. age> 18} dB. Users. Find (f );
Sort ()
ASC in ascending order of age
DB. Users. Find (). Sort ({age: 1 });
Desc in descending order of age
DB. Users. Find (). Sort ({age:-1 });
Limit the number of returned records ()
5 records are returned.
DB. Users. Find (). Limit (5 );
3 records are returned and information is printed.
DB. Users. Find (). Limit (3). foreach (function (User) {print ('My age is '+ User. Age )});
Result
My age is 18
My age is 19
My age is 20
Restrict the start point skip of the returned record ()
Returns 5 Records (limit 3, 5) starting from 3rd records)
DB. Users. Find (). Skip (3). Limit (5 );
Number of query records count ()
DB. Users. Find (). Count ();
DB. Users. Find ({age: 18}). Count ();
The number of all records in the User table is not 5.
DB. Users. Find (). Skip (10). Limit (5). Count ();
If you want to return the number of records after the limit, use count (true) or count (not 0)
DB. Users. Find (). Skip (10). Limit (5). Count (true );
Group ()
Assume that the test table only uses the following data
{Domain: "www.mongodb.org"
, Invoked_at: {d: "2009-11-011-03", T: "17:14:05 "}
, Response_time: 0.05
, Http_action: "Get/display/docs/aggregation"
}
Count: Count (*), total_time: sum (response_time), avg_time: total_time/count;
DB. Test. Group (
{Cond: {"invoked_at.d": {$ GT: "2009-11", $ LT: "2009-12 "}}
, Key: {http_action: true}
, Initial: {count: 0, total_time: 0}
, Reduce: function (Doc, out) {out. Count ++; out. total_time + = Doc. response_time}
, Finalize: function (out) {out. avg_time = out. total_time/out. Count}
});
[
{
"Http_action": "Get/display/docs/aggregation ",
"Count": 1,
"Total_time": 0.05,
"Avg_time": 0.05
}
]

Iv. Summary

A) When MongoDB creates a database, it only requires the user's new database name and then dB. createcollection ("new table") [adds a new table for the first time] creates a database and a tablespace. A single command does not reflect the creation of a database.

B) when inserting table data, the table data fields can be more or less. This may be because MongoDB uses JSON to store data. Distinctive.

C) create three files for each database: database name. 0, database name. 1, and database name. ns. Open the three files in tail and check that the database name. 0 is used to store table data. The database name. 1 is unclear. The database name. NS is used to store table space and indexes.

d) MongoDB commands use the camel-Type COMMAND method, which can be uppercase and lowercase for a while. It is a little uncomfortable in the command line operation mode. All in lower case!

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.