1. Initial knowledge
The contact with MongoDB is due to the recent work with MongoDB doing data storage. I've heard about this NoSQL database before, but have never tried to use it for development. Take advantage of this opportunity to learn the NoSQL database. Every time I hear this name, I always think, why its logo is not a mango it!
2. Introduction
MongoDB is an open-source, cross-platform, document-oriented database written in C + + and is a NoSQL database. MongoDB abandons the table-based data structure in the traditional database and uses a document structure similar to the JSON format to store the data (this format is called Bson in MongoDB). MongoDB was originally developed by the company in 2007 by the 10gen (2013 renamed MongoDB INC) and open source in 2009.
3. Terminology
Before using MongoDB, it is necessary to understand the terms in the next MongoDB. Just as we use relational data, we have to understand what a table is and what a database (Schema) is.
3.1 Document
is the basic unit of data in MongoDB, similar to the concept of "line" in a relational database we are familiar with. In MongoDB,Document is a collection of ordered sets of key-value pairs. In programming languages, data structures such as map,hashtable and dictionary can be matched to this storage structure, for example, for JavaScript, whose objects are represented in this format.
{"Greeting": "Hello, world!"}
3.2 Collection
In MongoDB, a Collection is made up of a set of Document . Similarly, document corresponds to the concept of "line" in relational data, so collection corresponds to the concept of "table" in the database.
The document stored in collection must contain a "_id" field that represents the primary key of the record, and MongoDB automatically generates one if it is not included in the document.
The collection in MongoDB has a specific dynamic pattern, meaning that the document contained in a collection can have a different structure. This differs from a relational database in that the format of each row in the table is consistent across the relational data. In MongoDB, a collection can contain document in a different format:
{"Greeting": "Hello, world!" } {"foo": 5}
3.3 Database
Like a series of document composed of a collection, in MongoDB, a series of collection formed the database. A database has its own permissions control, and each database is stored in a separate data file.
4. Use 4.1 to start MongoDB service
Start MongoDB with the "Mongod" command provided by MongoDB
Mongod--dbpath mongodb/data/
The "--dbpath" option specifies the data store directory for the MongoDB service. If you do not specify any parameters, the default data store directory is "/data/db". When the command is executed, some information about the current system and some information about the MongoDB service are output, including the port number of the service: 27017.
4.2 Using MONGO Shell to access MongoDB service
The MONGO shell is a client that interacts with the MongoDB service and is also a JavaScript interpreter. The data in MongoDB can be manipulated by the public MONGO shell. MONGO shell is installed with MongoDB, you can find the "MONGO" command under directory "{MongoDB installation directory}/bin/".
Start MONGO Shell
Mongo
If no parameters are specified, the localhost:27017 mongodb service is accessed by default. Once we have a local MongoDB service, we can connect to the server directly:
The default database is "test", and we can view the currently active database through the variable name "DB":
Using the "Use" command, you can switch the database
If you exit the MONGO shell after you have finished your work, you can enter the "exit" command to exit.
With the help command, you can get the MONGO shell helpful information:
4.3 crud operations via MONGO Shell
Here's a brief description of how to do a simple crud operation with the MONGO shell.
Suppose we are currently in the blog database.
4.3.1 insert operation
We add an article to the "POST" collection of our Blog database:
4.3.2 Query operation
In the previous step, we added a document to collection "blog", now we need to query this article:
4.3.3 Update operation
Now, we need to modify the content of this blog:
4.3.4 Delete operation
Finally, remove the post we added above:
5. Summary
Here, we just have a simple understanding of MongoDB, as to its crud syntax, and the use of MONGO shell, and listen to tell ~
First knowledge of MongoDB