MongoDB Learning Notes (Getting started)

Source: Internet
Author: User
Tags findone mongodb server

First, the documentation considerations:
1. Key-value pairs are ordered, such as: {"name": "Stephen", "Genda": "Male"} Not equal to {"Genda": "Male", "name": "Stephen"}
2. The document information is case sensitive, such as: {"name": "Stephen"} does not equal {"name": "Stephen"}
3. The 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 collections:
1. It is very disastrous for developers to store documents of various schemas in a single set. Because after you get the results of the query, you need to manually write code to filter the different types of documents.
2. The query efficiency will be reduced. Imagine that a pattern's document has a relatively small amount of data, and if you still put it in a large, universal set, its query efficiency is bound to be significantly lower than putting it in a separate collection.
3. When you create an index, the utilization of the index is more efficient if all the documents are in the same mode.

Iii. Naming considerations for collections:
1. The collection name cannot be an empty string "".
2. Do not start with the system, which is generally reserved for systems, such as the System.users collection where the database's user information is saved, and the System.namespace collection retains information about the database collection.
3. Do not include the ' $ ' character in the collection name.
4. Subcollections are just a good collection planning method, such as Blog.posts and blog.anthurs, in fact they have nothing to do with the collection of blogs, even the collection of blogs can not exist.

Iv. Database:
Multiple databases can exist in the same MongoDB server, and each different database is stored in a different file. And because the database name and file name are bound, there are some limitations to the database name.
1. Cannot be a null character "".
2. All lowercase and not exceeding 64 bytes.
3. Must not contain illegal characters in the name of the file name.
4. The admin database is the management database, and if a user is in that database, he automatically inherits all of the database permissions. Some specific server commands can also be run from this database only.
5. Local this database is never copied, only for storing any collection that is limited to a local single server.
6. The database name, the collection name, represents the fully qualified name of the collection, and its length does not exceed 121 of its own.

Five: The start of MongoDB:
1. Directly execute mongod, without any command-line arguments, the server's host must contain the/data/db directory, and for Windows, the default directory is the \data\db of the drive where the service program resides. such as the D-Disk, the D:\data\bin. The default listening port is 27017.
2. MongoDB comes with 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:

> x = 200
200
> X/5
40
--You can also call the JavaScript standard library.
> New Date ("2012/05/05")
Isodate ("2012-05-04t16:00:00z")
> "Hello World". Replace ("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 a MongoDB document in the shell client, such as:
> 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 document in the shell client, such as:
> 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:
--You need to update the contents of the post variable first by adding a comments key, whose value is an empty array.
> post.comments = []
[ ]
the first parameter of the--update is the condition, and the second parameter 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 the shell client, such as:
--Clears all data in the collection if there are no conditions in remove.
> Db.blog.remove ({title: "My blog post"})
> Db.blog.findOne ()
Null

Six, the shell of the use of tips:
> Show DBS -Displays the database name.
> Show Collections --Display the collection name
> Show users--Display user name
> db.help () --a way to list the database.
> db.blog.help () --Lists the methods on the blog collection.
> Db.blog.update --The JavaScript implementation code that can look directly at the Update method.

MongoDB Learning Notes (Getting started)

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.