MongoDB Application Detailed

Source: Internet
Author: User

MongoDB is a software used to store management data.

He is a C/s architecture software, is a network type of software
If you use MongoDB, you first need to open the service side of MongoDB, and then connect to the server through the client software

1. To create a database for the current application:
Database-excel
Table-sheet1-Distinguish a collection of data that stores the same type
Row-document-A record
Column-field-Columns, fields


MongoDB data type

The following table is a few of the data types commonly used in MongoDB.
Data type description
string literal. Data types commonly used to store data. In MongoDB, the UTF-8 encoded string is legal.
Integer integer value. Used to store numeric values. Depending on the server you are using, it can be divided into 32-bit or 64-bit.
A Boolean Boolean value. Used to store Boolean values (True/false).
Double-precision floating-point value. Used to store floating-point values.
Min/max keys compares a value to the lowest value and the highest value of the BSON (binary JSON) element.
Arrays is used to store an array or list or multiple values as a single key.
Timestamp time stamp. Record when the document was modified or added.
object is used for inline documents.
Null is used to create a null value.
Symbol symbols. The data type is basically the same as the string type, but unlike it, it is typically used in languages with special symbol types.
Date DateTime. Use the UNIX time format to store the current date or time. You can specify your own datetime: Create a Date object and pass in the month-date information.
Object ID. The ID used to create the document.
Binary data binary. Used to store binary data.
The code type. Used to store JavaScript code in a document.
Regular expression regex type. Used to store regular expressions.


Command:
DB: View your current database
Show DBS: Display a list of all databases
Use <db name >: Choose to enter the specified DB, if the database does not exist, it will be created first, if there is no data inside the data, may not show

Insert:
Db.<collection_name>.insert ({}): Inserts a piece of data into the specified collection (table), and if the collection does not exist, it is automatically created


Update:
Db.<collection_name>.update (
<query>,
<update>,
{
Upsert: <boolean>
Multi: <boolean>
Writeconcern: <document>
}
)

The query:update query condition, similar to that in SQL Update query, is behind.
Update:update objects 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.

Db. Users.update ({username: ' Leo '},{$set: {age:36}});

Inquire
Db. Collection_name.find ()
Db. Collection_name.find (). Pretty (): Format print data

 1  equals {<key>:<value>} db.col.find ({"By": "Rookie Tutorial"}). Pretty () where by = ' Rookie tutorial ' 2  less than {<key>:{$lt: <value>}} db.col.find ({"likes": {$ LT:50}). Pretty () where likes < 503  less than or equal to {<key>:{$lte: <value>}} Db.col.find ({"likes": {$lte:). Pretty () where likes <= 504  greater than {<key> : {$gt: <value>}} db.col.find ({"likes": {$gt: {)}). Pretty () where likes > 505  greater than or equal to {<key>:{$gte: <value>}} db.col.find ({"likes": {$gte:]}). Pretty () where likes >= 506  is not equal to {<key>:{$ne: <value>}} db.col.find ({"likes": {$ne:]}). Pretty () where likes! = 
1  and 2 Db.col.find ({key1:value1, key2:value2}). Pretty () 3 or 4 Db.col.find ({$or: [{key1:value1},{key1:value1}]}); 5 6 7 Delete 8 db.col.remove ({})

Limit (number): Gets the record of the specified bar
Skip (number): Specifies how many records to skip

Limit+skip can achieve the effect of paging
For example, there are 100 data
Show 5 per page
Need to display the third page of data
Limit (5). Skip (10);


Add field indexes to make queries faster
Db.<collection_name>.ensureindex ({key:1})

Fuzzy query
Using the regular
Db.<collection_name>.find (KEY: {$regex: ' regular string '})
Db.<collection_name>.find (KEY:/Regular/)

Add to
Inquire
Modify
Delete

Conditions
Sort
Restrict limit
Skip
Fuzzy matching

1 Add 3 Item Info2Db. Goods.insert ({name: ' iphone ', ' Price ': 4000});3Db. Goods.insert ({name: ' imac ', ' price ': 10000});4Db. Goods.insert ({name: ' iphone6 ', ' Price ': 5000});5Db. Goods.insert ({name: ' iphone6s ', ' price ': 6000});6 Modify7Db. Goods.update ({name: ' iphone '}, {$set: {price:2000}} );8 Delete9Db. Goods.remove ({name: ' iphone6s ')});Ten Enquiry OneDb. Goods.find ({name: ' iphone ')}). Pretty (); ADb. Goods.find ({name:/iphone/}). Pretty (); -Db. Goods.find ({price: {$gt: 3000}}). Pretty (); -Db. Goods.find (). Limit (1). Pretty (); theDb. Goods.find (). Skip (1). Pretty (); -Db. Goods.find (). Limit (1). Skip (1). Pretty (); -Db. Goods.find (). Sort ({price:-1}). Pretty ();

MongoDB Application Detailed

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.