"MongoDB" 3. Detailed command Set

Source: Internet
Author: User
Tags findone

"Note: MongoDB automatically sets the _id field as the primary key"

--------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------->

Don't say a word, just start knocking at the command!

"Note 1:mongodb command end do not hit; It will be an error. "

Note 2:robomongo the shortcut key to execute the command is F5 Stop execution shortcut is F6 "

1.show DBS

Show a list of all data

2.db

Displays the current database object or collection

3.use

Can connect to a specified database

4. Db.createcollection ("Mynewcollection", {capped:true,size:1024})

Fixed Size Collection "collection size unit is byte" "Capped:true stands for fixed size"

5.db.collection.stats ()

See the details of a collection in the current DB

For example:

Db.users.stats ()

1 {2 " ns": "Onedb.users",3"Count" :4,4"Size": the,5"Avgobjsize": -,6"Numextents":1,7"Storagesize":8192,8"Lastextentsize":8192.0,9"Paddingfactor":1.0,Ten"Paddingfactornote": "Paddingfactor isUnused andunmaintainedinch 3.0. It remains hard coded to 1.0  forCompatibility only.", One"UserFlags":1, A "capped": false, -"Nindexes":1, -"Totalindexsize":8176, the "indexsizes": { -"_id_":8176 -     }, -"OK":1.0 +}
View Code
1 A detailed explanation of some of the parameters:2 NS: The namespace of the collection, which can be understood as the collection name3          Count: Total number of documents in the collection4 Size : The amount of data in the collection that is in space, not including the index, in bytes. 5 avgobjsize: Average amount of space occupied by the image6 storagesize: The storage space allocated to the entire collection, which is not reduced when the document in the collection is deleted. 7 numextents: Contiguous allocation of data blocks8 Nindexes: The number of indexes, with at least one _id index per collection. 9 lastextentsize: The size of the most recently allocated blockTen Paddingfactor: This parameter is not very clear, later added.  One totalindexsize: Sum of all index sizes AIndexsizes: Lists all index fields for the collection, along with the index size.
View Code

6.db. Dropdatabase()

Delete current Database

7.use DbName

If you specify to this database, there is no creation of this database but after creation is not displayed in the database list by default, you need to insert a piece of data to display it.

"NOTE: Insert data should be enclosed in curly braces {}"

For example:

Use Onedb
Db
Db.users.insert ({_id:1, "name": "Zhang Sanfeng", "Age": 23})

8.db. Collection. Drop()

Delete Collection

For example:

9. Defining variables to insert data

Document = {_id:1, "name": "Zhang San", "Age": $, "url": "Www.baidu.com"}
Db.users.save (document)

10. Updating a single piece of data

db.. Update ( <query> <update>, {< Span class= "PLN" > Upsert: <boolean> ,  Multi: <boolean> , Writeconcern: < Document> }) 
    • Query: The search condition for update, similar to where in SQL update query.
    • Update: Update object and some updated operators (such as $, $inc ... ) can also be understood as the SQL update query within the set after the
    • upsert : Optional, this parameter means that if there is no record of update, insert objnew,true as INSERT, default is False, do not insert.
    • multi : Optional, mongodb default is False, only update the first record found, if this parameter is true, will be found on the condition of a number of records update all.
    • writeconcern : Optional, throws an exception level.

For example:

Db.users.update ({_id:1},{$set: {"url": "Https://home.cnblogs.com/u/sxdcgaq8080/"}},{upsert:true,muti:true})
Db.users.find ()

11.db.users.find () query "various queries"

The Find () method displays all documents in an unstructured way

Db.users.find (). Pretty ()

If you need to read the data in an easy-to-read way, you can use the pretty () method; the pretty () method to display all the documents in a formatted manner

Db.users.findOne ()

FindOne () method, which returns only one document

Operation format Example similar statements in an RDBMS
Equals {<key>:<value>} db.users.find({"name":"张三"}).pretty() where by = ‘张三‘
Less than {<key>:{$lt:<value>}} db.users.find({"age":{$lt:23}}).pretty() where likes < 23
Less than or equal to {<key>:{$lte:<value>}} db.users.find({"age":{$lte:23}}).pretty() where likes <= 23
Greater than {<key>:{$gt:<value>}} db.users.find({"age":{$gt:23}}).pretty() where likes > 23
Greater than or equal to {<key>:{$gte:<value>}} db.users.find({"age":{$gte:23}}).pretty() where likes >= 23
Not equal to {<key>:{$ne:<value>}} db.users.find({"age":{$ne:23}}).pretty() where likes != 23

  for example: Equals-->< Span style= "font-family:"microsoft yahei"; Font-size:14px ">db.users.find ({" Age ": +})  



Less than-->db.users.find ({" Age ": {$lt: +}})

Less than or equal to-->db.users.find ({"Age": {$lte: 25}})

and condition Query

For example: Db.users.find ({"Age": {$lte: +}, "name": "Zhang San"})

Two key-value on the line.

or condition Query

For example: Db.users.find ({$or: [{"Name": "Zhang San"},{"Age": 25}]})

Note: You need to use {$or: [{key:value},{key:value}]} This format "

situations where and and or are mixed

For example: Db.users.find ({"Age": {$gte: $}, $or: [{_id:1},{"name": ""}]})

Analyze the structure:

Db.users.find ({
"Age": {$gte: 23},
$or: [
{_id:1},
{"Name": "Vince"}
]
})




"MongoDB" 3. Detailed command Set

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.