MongoDB Study Notes (Getting Started)

Source: Internet
Author: User
Tags findone mongodb server

I. Notes:
1. key-value pairs are ordered, for example, {"name": "Stephen", "Genda": "male"} is not equal to {"Genda": "male ", "name": "Stephen "}
2. The document information is case sensitive, such as: {"name": "Stephen"} not equal to {"name": "Stephen "}
3. Document Information is of different types, such as: {"Age": 30} not equal to {"Age": "30 "}
4. Duplicate keys cannot appear in the document, for example, {"name": "Stephen", "name": "Liu "}

 

Ii. necessity of using multiple sets:
1. It is a disaster for developers to store documents of various modes in one collection. Because after obtaining the query results, you need to manually writeCodeFilter different types of documents.
2. the query efficiency will be reduced. Imagine that a certain mode of document has a relatively small amount of data. If it is still put into a general collection, the query efficiency will be significantly lower than putting it into an independent collection.
3. When creating an index, if all documents are in the same mode, the index utilization will be more efficient.

 

Iii. Considerations for set naming:
1. The set name cannot be a Null String "".
2. do not start with system. This is generally reserved for the system, such as system. the users collection saves the user information of the database, while the system. the information of the database set is retained in the namespace set.
3. Do not include the '$' character in the Set Name.
4. subsets are only a good way to plan collections, such as BLOG. Posts and blog. anthurs. In fact, they have no relationship with the blog set, and even the blog set may not exist.

 

Iv. database:
Multiple databases can exist on the same MongoDB server, and different databases are stored in different files. Because the Database Name and file name are bound, there are some restrictions on the database name.
1. It cannot be a NULL Character "".
2. All are in lowercase and cannot exceed 64 bytes.
3. Do not contain invalid characters in the name of a file.
4. the admin database is used to manage databases. If a user is in the database, the admin database automatically inherits all database permissions. Some specific server commands can only be run from this database.
5. The local database will never be copied, but is used to store any set of local servers.
6. Database Name. Set Name, which indicates the fully qualified name of the set. Its length should not exceed 121.

 

V. MongoDB startup:
1. Run mongod directly. Without any command line parameters, the server host must contain the/data/DB directory. For Windows, the default directory is the service.Program\ Data \ dB of the drive. For example, 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. For example, Mongo. This shell tool can directly execute simple mathematical operations. For example:

> X = 200
200
> X/5
40
-- You can also call the standard library of JavaScript.
> New date ("2012/05/05 ")
Isodate ("2012-05-04t16: 00: 00Z ")
> "Hello world". Replace ("world", "MongoDB ")
Hello MongoDB
-- Define and call 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, for example:
> 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 documents on the shell client, such:
> 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 shell, such:
-- You need to update the content of the post variable first, that is, add a comments key, and its value is an empty array.
> Post. Comments = []
[]
-- The first parameter of 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 the file in the shell client, for example:
-- If no conditions exist in remove, all data in the set is cleared.
> DB. Blog. Remove ({Title: "My blog post "})
> DB. Blog. findone ()
Null

 

6. Tips for using shell:
show DBS -- display the database name.
show collections -- display the Set Name
show users -- Display User Name
dB. help () -- lists database methods.
dB. blog. help () -- list methods on the blog set.
dB. blog. update -- you can directly view the Javascript implementation code of the update method.

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.