Basic knowledge of MongoDB tutorials _mongodb

Source: Internet
Author: User
Tags findone mongodb mongodb server

First, documentation considerations:

1. Key-value pairs are ordered, such as: {"name": "Stephen", "Genda": "Male"} is not equal to {"Genda": "Male", "name": "Stephen"}
2. Document information is case sensitive, such as: {"name": "Stephen"} is not equal to {"name": "Stephen"}
3. Document information is type-sensitive, such as: {"Age": 30} is not equal to {"Age": "30"}
4. Duplicate keys cannot appear in the document, such as: {"name": "Stephen", "Name": "Liu"}

Second, the need to use multiple sets:

1. It would be disastrous for developers to keep documents of various schemas in a single collection. Because after you get the query results, you need to write code to filter the different types of documents manually.
2. The query efficiency will be reduced. Imagine that a schema has a relatively small number of documents, and if you still put it in a large, general-purpose set, its query efficiency will be significantly less efficient than putting it in a separate collection.
3. When you create an index, the utilization of the index is more efficient if all documents are in the same mode.

Third, the collection of naming considerations:
1. The collection name cannot be an empty string "".
2. Do not start with system, which is typically reserved for systems such as the System.users collection, where the database's user information is saved, while the System.namespace collection retains information about the database collection.
3. Do not include the ' $ ' character inside the collection name.
4. Sub-sets are just a good set planning way, such as blog.posts and blog.anthurs, there is no relationship between them and the blog collection, even the blog collection can not exist.

Four, the database:

Multiple databases can exist in the same MongoDB server, and each of the different databases is stored in different files. And because the database name and file name are bound, there are some restrictions on the database name.
1. Cannot be a null character "".
2. All lowercase and not exceeding 64 bytes.
3. Must not contain the illegal characters in the name of the filename.
4. The admin database is the administrative database, and if a user is in that database, he will automatically inherit all database permissions. Some specific server commands can only be run from this database.
5. The local database is never replicated, but is used to store any collection that is limited to a single server locally.
6. Database name. The name of the collection, which represents the fully qualified name of the collection and does not exceed 121 of its own length.

Five: MongoDB start:

1. The direct execution of Mongod can be, in the absence of any command-line parameters, the server must contain the host/data/db directory, for Windows, the default directory is the service program is the drive \data\db. such as D plate, then D:\data\bin. The default listener port is 27017.
2. MongoDB has a JavaScript Shell that can interact with MongoDB from the command line. such as: MONGO. This shell tool can perform simple mathematical operations directly. Such as:

Copy Code code as follows:

> x = 200
200
> X/5
40
-You can also invoke the standard library of JavaScript.
> New Date ("2012/05/05")
Isodate ("2012-05-04t16:00:00z")
> "Hello World". Replace ("The World", "MongoDB")
Hello MongoDB
--Define and invoke custom JavaScript functions.
> function factorial (n) {
... if (n <= 1) return 1
... return n * factorial (n-1)
... }
> Factorial (5)
120

3. Insert the MongoDB document into the shell client, such as:

Copy Code code as follows:

> post = {"title": "My blog post", "Content": "Here's My Blog", "date": New Date ()}
{
"title": "My blog post",
"Content": "Here's My Blog",
"Date": Isodate ("2012-06-04t07:38:51.345z")
}
> Db.blog.insert (POST)
> Db.blog.find ()
{"_id": ObjectId ("4FCC661DE4BCBAC15B3D9E3A"), "title": "My blog post", "Content": "Here's My Blog",
"Date": Isodate ("2012-06-04t07:38:51.345z")}

4. Query the shell client for documents such as:

Copy Code code as follows:

> Db.blog.findOne ()
{
"_id": ObjectId ("4FCC661DE4BCBAC15B3D9E3A"),
"title": "My blog post",
"Content": "Here's My Blog",
"Date": Isodate ("2012-06-04t07:38:51.345z")
}

5. Update the document in the shell, such as:

Copy Code code as follows:

--You need to update the contents of the post variable first by adding a comments key with an empty array.
> post.comments = []
[ ]
The first parameter of the--update is a condition, and the second is the value to be updated.
> db.blog.update ({"title": "My blog post"}, Post)
> Db.blog.findOne ()
{
"_id": ObjectId ("4FCC661DE4BCBAC15B3D9E3A"),
"title": "My blog post",
"Content": "Here's My Blog",
"Date": Isodate ("2012-06-04t07:38:51.345z"),
"Comments": []
}

6. Delete in shell client, such as:
Copy Code code as follows:

--Clears all data in the collection if there are no conditions in the remove.
> Db.blog.remove ({title: "My blog post"})
> Db.blog.findOne ()
Null

Six, the shell uses the small skill:
Copy Code code as follows:

> Show DBS--Displays the database name.
> Show Collections--Display collection name
> Show Users--display user names
> Db.help ()--Lists the methods for the database.
> Db.blog.help ()--Lists the methods on the blog collection.
> Db.blog.update--You can see the JavaScript implementation code of the Update method directly.

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.