the basic concept of MongoDB
Document: Is the basic unit of data in MongoDB, similar to a row in a relational database.
Collections: Multiple Documents form a collection, similar to a table in a relational database.
Database: MongoDB When an instance can hold multiple independent databases, each database has its own collection and permissions.
Key: Each document has a special key "_id", which is unique in the collection in which it is located.
Multiple key-value pairs are ordered together as documents, such as:
{"Name": "Wangdh", "Age": "22"}
The above document contains two keys, name and age, with values of WANGDH and 22, respectively.
Description
1) The document's key-value pairs are ordered, that is, the following document is two different documents from the document above:
{"Age": "$", "name": "WANGDH"}
2) The key to the document is a string.
3) MongoDB not only distinguishes between types but also case-sensitive. That is, the following two documents are different:
{"Age": "$", "Age": 22}
{"Age": $, "Age": "22"}
4) The same document cannot have the same key, as the following document is not valid:
{"Age": "$", "Age": 22}
A collection is a set of documents, equivalent to a table of relational databases.
1) non-modal of the set
modeless means that the documents in a collection can be of various kinds, and the key-value logarithm of the document and the order of key-value pairs can be different. For example, the following document can exist in a collection: {"Age": "$", "Age": 22}, {"Name": "WANGDH"}
But by the case, for ease of use, a collection tends to only store schema-consistent documents.
2) Sub-set: Through "." A separated subset of characters by namespace, such as a blog-capable app may contain two collections: Bolg.posts, Blog.authors
Multiple documents make up a collection, and multiple collections form a database. A MongoDB instance can host a database, each with independent permissions control.
Note: The database name will eventually become a file in the file system.
1) built-in database
Admin: From a privilege point of view, equivalent to the root database, if a user is added to this database, the user automatically inherit all the permissions of the database.
Local: This data is never copied and can be used to store any collection that is limited to a local single server.
Config: When MONGO is used for sharding settings, the database holds information about the Shard.
2) namespaces
Put the name of the database in front of the collection, get the fully qualified name of the collection, and become the namespace. For example, if there is a Blog.posts collection in the test database, the namespace of the collection is: Test.blog.posts. The namespace must not exceed 121 bytes in length.
The document stored in MongoDB must have a "_id" key, which can be any type, and the default is the Objectid object. In a collection, each document has a unique "_id" key to ensure its uniqueness.
1) ObjectId
Using 12 bytes of storage space, two hexadecimal digits per byte, is a 24-bit string. The first 4 bytes are timestamp, accurate to the second, the next 3 bytes are the unique identifier of the host (via the hash value of the hostname), the next 2 bytes are the process identifier (PID) that produced the Objectid, and the last 3 bytes are an auto-growth counter.
The first 9 bytes guarantee that the Objectid generated by the same second machine is unique, and the last 3 bytes ensure that the same process is different from the Objectid produced in the same second.
MongoDB Shell
MongoDB comes with a JavaScript shell that can interact with the MongoDB instance from the command line.
Under Windows platform, you can start the shell by running the MONGO file under Shell;linux by running the Mongo.exe file under the bin directory on the command line. The shell will automatically connect to the MongoDB server at startup, so be sure to start mongod before using the shell.
is the shell a standalone? MongoDB client, when turned on, the shell will connect to the MONGODB server's test database and assign this database connection to the global variable db. The database can be converted by using the use command, which is created if the database does not exist WANGDH.
- Basic operations in the shell
The basic operations of a general database are: Create, read, update, delete (CRUD).
1) Create insert
The Insert function adds a document to the collection. For example, the document to be inserted is stored first through the local variable T1, and then through the global variable db. Insert (T1) inserts the document T1 into the specified collection (created automatically when the collection does not exist).
>t1={"name": "Wangdh", "Age": 22}
>db.user.insert (T1)
2) Read find
The Find function returns all the documents in the collection, and the FindOne function returns a document.
For example, Db.user.find () reads the document you just inserted.
Two functions can accept action conditions in the form of a query document, and the shell automatically displays a maximum of 20 matching documents when using find
3) Updating update
The update function accepts at least two parameters: the first is the qualification to update, and the second is a new document. Such as:
>newname={"name": "NewName"}
>db.user.update ({"Name": "Wangdh"}, NewName)
4) Delete Remove
The Remove function permanently deletes the document from the database, deleting all documents within a collection if the parameter is not applicable, for example: Db.user.remove ()
MongoDB Learning Note -02 Getting started with MongoDB