1. Features of MongoDB:
(1) Easy to use
MongoDB is a document-oriented database, a non-relational database. by embedding documents and data in a document, the object-oriented approach enables a single record to represent complex hierarchical relationships . The key and value of the document is no longer a fixed type and size.
(2) Easy to expand
MongoDB is designed to scale out by partitioning the data across more machines. The document-oriented data model makes it easy to split data between multiple servers . MongoDB is able to automate the processing of cluster data and load , automatically reassign documents, and route user requests to the correct machine.
(3) Rich features
1) Index
MongoDB supports generic two-level indexes , allows for a variety of quick Queries , and provides unique indexes, composite indexes, geospatial indexes, and full-text indexes .
2) Aggregation
MongoDB supports converged pipelines , where users can create complex aggregations through simple fragments and automatically optimize through the database.
3) Special collection types
MongoDB supports collections of limited time, suitable for data that will expire at some point, such as sessions , and also supports fixed-size collections for storing recent data, such as logs .
4) file storage
MongoDB supports very easy-to-use protocols for storing large files and file meta data .
2. MongoDB Basics
(1) Documentation
The document is the core concept of MongoDB, and the document is an ordered set of key-value pairs . The key of the document cannot contain a/s ( null character), and the "." and "$" characters can only be used in a specific environment , and both are reserved. MongoDB is not but a differentiated type, and is case-sensitive and cannot contain duplicate keys , and the key/value pairs in the document are ordered : {"x": 1, "Y": 2} and {"Y": 2, "X": 1} are different.
(2) Collection
A collection is a set of documents . A document can be considered a row of a relational database, and a collection is the equivalent of a single table.
1) Dynamic mode
The collection is in dynamic mode , i.e. {"Greeting": "Hello, world!"} {"Foo": 3} can exist in the same collection.
2) naming
The collection cannot be an empty string (""); \ n characters (null characters)cannot be included in the collection; The collection cannot be "syste,." Begins with a prefix reserved for the system collection (for example, system.users saves the user information for the database, while the System.namespaces collection holds information about all the database collections ) The user-created collection cannot contain the reserved character "$" in the collection name.
subcollections are used "." Separating subsets of different namespaces and using subcollections to organize data is highly efficient.
(3) database
1) database name must be satisfied: cannot be an empty string (""); basically, only letters and numbers in ASCII are used; database names are case-sensitive; Database names are up to 64 bytes
2) Reserved database name:
admin:root database , with all database permissions, and some specific server-side commands can only be run from the Admin database (list all databases or shut down the server)
Local : Never replicate , and all local collections on a single server can be stored in this database.
config: for sharding settings .
(4) Start MongoDB
Run the Mongod command to start the database server. Note: Mongod uses the default data directory/data/db when no parameters are available, so before you start, create a data Catalog/data/db to ensure that the directory has write permissions.
At startup, the server prints version and system information and listens to port 27017 by default. Mongod also launches a basic HTTP server with a listening number of 28017, which can be used to obtain management information for the database through http://localhost:28017.
3. Windows install MongoDB, refer to: Install MongoDB under windows and use collation
4. Shell Basic operation
MongoDB Shell,shell is a full-featured JavaScript interpreter that can run any JavaScript program.
#简单数学运算> x= $ $> x/5 +# using JS standard library> Math.sin (math.pi/2)1> New Date ("2016/01/20") Isodate ("2016-01-19t16:00:00z")>"Hello,world". replace (" World","MongoDB") hello,mongodb# definition call JS function>functionfactorial (n) {...if(n<=1) return1;... return n*factorial (n1);... }> Factorial (5) ->
Note: press ENTER three times in a row to cancel a command that has not been entered for completion .
(1) At startup, theshell (MongoDB client) connects to the MONGODB server's test database and assigns the database connection to the global variable db, which is the main entry point for accessing MongoDB through the shell .
#查看db当前指向那个数据库> dbtest>#选择数据库> Use foobarswitched to db foobar> Dbfoobar>
With the DB variable, you can access the collection , such as Db.baz, which returns the Baz collection of the current database.
(2) Create
The Insert function adds a document to the collection . Example: Create a post local variable to store a blog post that represents our document with the key "title", "Content", and "date".
# define local variable post> post={"title":"My Blog Post",... "content":"Here ' s my blog post.",... "Date": New Date ()} { "title":"My Blog Post", "content":"Here ' s my blog post.", "Date": Isodate ("2016-01-20t13:29:51.882z"}# Call Insert to save it to the blog collection>Db.blog.insert (POST) writeresult ({"ninserted":1}) #查看保存的数据> Db.blog.Find(){ "_id": ObjectId ("569f8c0c0124e675dc0e4f0f"),"title":"My Blog Post","ConteNt" : "Here's my blog post. "," date ": Isodate (" 2016-01-20t13:29:51.882z ")}>
(3) Read
the Find and FindOne methods can be used to query a document in a collection and can be used for FindOne if you want to view only one document. Both can accept a query document as a qualification, and when you use Find, the shell displays up to 20 matching documents .
>Db.blog.findOne () {"_id": ObjectId ("569f8c0c0124e675dc0e4f0f"), "title":"My Blog Post", "content":"Here ' s my blog post.", "Date": Isodate ("2016-01-20t13:29:51.882z")}>
(4) Update
The update function is used for updates and accepts at least two parameters: qualification (used to match documents that need to be updated) and new documents . Example: To add a comment function to a previously written article, you need to add a new key.
# Modify the variable post to add"Comments"Key> post.comments=[] []# performs the update, replacing the title with the new post"My Blog Post"the article> Db.blog.update ({"title":"My Blog Post"},post) Writeresult ({"nmatched":1,"nupserted":0,"nmodified":1}) # View the update results>Db.blog.findOne () {"_id": ObjectId ("569f8c0c0124e675dc0e4f0f"), "title":"My Blog Post", "Content":"Here ' s my blog post.", "Date": Isodate ("2016-01-20t13:34:19.849z"), "Comments" : [ ]}>
(5) Delete
Use the Remove method to permanently delete questions and answers from the database . If no parameters are used, all documents within the collection are deleted . You can accept a document as a qualification as a parameter .
> db.blog.remove ({"title":"My blog Post" "nremoved"1 })>
Windows MongoDB installation and use collation
MongoDB Learning Note (1): About MongoDB