MongoDB authoritative guide (1)-getting started

Source: Internet
Author: User
Tags findone mongodb download relational database table

Recently, the company has considered restructuring data. Because the data is complicated, I think MongoDB is a very suitable choice and I will understand it,

At first glance, hey, it's quite interesting, so I got this series of Reading Notes. If anything is inappropriate, please kindly advise.

In addition, I do not translate some specialized words. One is not good at translation, and the other is that translation will impede understanding.

Book ebook download link http://download.csdn.net/source/3399006

MongoDB Download Links http://www.mongodb.org/downloads

Bytes --------------------------------------------------------------------------------------------------------------

Install

After decompression, it is OK. By default, MongoDB uses the data folder in the disk root directory and the data \ dB folder,

These two folders need to be created manually. If you want to use other paths, you need to specify the path when starting MongoDB.

Run

Run the supervisor to open the control window, navigate to the BINLOG of mongodb, and run mongod.exe. Then the server starts up and press Ctrl-C to end the program.

Open another control window, navigate to the bindirectory of mongodb, run cmd.exe to start the shell, and then link to the server. By default, it is connected to the test database.

Bytes -------------------------------------------------------------------------------------------------------------

Okay. Let's talk about how MongoDB works. Here is the second chapter.

1. Some Basic Concepts

Document: The basic unit of data in MongoDB, which is equivalent to rows in a relational database.

Collection: equivalent to a relational database table, but not defined in the Data Structure

Each MongoDB instance can run multiple databases, and each database has its own collection and permission control.

MongoDB has a powerful JavaScript shell for managing databases and operating data.

Each document has a special key: "_ id", which is unique in the collection.

Document

Document is a set of ordered key/value pairs that use JSON-style data.

{"foo" : 3, "greeting" : "Hello, world!"}

Key is a UTF-8 string, value can be a lot of types, or even an embedded document.

Collection

Collection is a set of documents that have no structure defined, so you can store any document in a collection.

Subcollection

A habitual way of organizing collections, separated by a. Sign, like a namespace. For example, a blog is used in a program, which may contain a collection called blog. post and another collection are called blogs. authors is only for the purpose of organizing content. They seem to be a subset of a blog. In fact, they have nothing to do with, and even a blog may not exist.

Database

A MongoDB instance can run multiple databases, which are completely independent from each other. Each database has its own permissions, and each database is stored in different files on the disk.

2. MongoDB Shell

Shell itself is a javascript interpreter. Let's take a look.

Mathematical operations are supported.

> X = 200
200
> X/5;
40

You can use a standard JavaScript Library

> Math. Sin (math. PI/2 );
1
> New date ("2010/1/1 ");
"Fri Jan 01 2010 00:00:00 GMT-0500 (EST )"
> "Hello, world! ". Replace (" world "," MongoDB ");
Hello, MongoDB!

You can even define JavaScript Functions.

> Function factorial (n ){
... If (n <= 1) return 1;
... Return N * factorial (n-1 );
...}
> Factorial (5 );
120

The ability to execute JavaScript is indeed cool, of course, this is not all of the Shell functions.

  • Use use naming to switch Databases
> Use foobar
Switched to DB foobar

Then you can view the database variables to see what the current database is.

> DB
Foobar
  • Use the insert function to insert a document into a collection object.

First create a local variable named post

> Post = {"title": "My blog post ",
... "Content": "Here's my blog post .",
... "Date": new date ()}
{
"Title": "My blog post ",
"Content": "Here's my blog post .",
"Date": "sat Dec 12 2009 11:23:21 GMT-0500 (EST )"
}

Then insert it into the collection called blog.

> DB. Blog. insert (post)

Then we can use the find function to check the content in the blog.

> DB. Blog. Find ()
{
"_ Id": objectid ("4b23c3ca7525f35f94b60a2d "),
"Title": "My blog post ",
"Content": "Here's my blog post .",
"Date": "sat Dec 12 2009 11:23:21 GMT-0500 (EST )"
}

A key named "_ id" is automatically added.

  • Find returns the document in the collection. If you only want to view one, use findone> dB. Blog. findone ()
    {
    "_ Id": objectid ("4b23c3ca7525f35f94b60a2d "),
    "Title": "My blog post ",
    "Content": "Here's my blog post .",
    "Date": "sat Dec 12 2009 11:23:21 GMT-0500 (EST )"
    }

Both find and findone can have query conditions, which will be described in Chapter 4th.

  • Update document

The update function must have at least two parameters. The first is the condition, and the second is the new document.

First add a key called comments to the post variable and give it an empty array for value.

> Post. Comments = []
[]

Execute the update and replace the document whose title is "my blog post ".

> DB. Blog. Update ({Title: "My blog post"}, post)

View results

> DB. Blog. Find ()
{
"_ Id": objectid ("4b23c3ca7525f35f94b60a2d "),
"Title": "My blog post ",
"Content": "Here's my blog post .",
"Date": "sat Dec 12 2009 11:23:21 GMT-0500 (EST )"
"Comments": []
}
  • Use remove to delete document> dB. Blog. Remove ({Title: "My blog post "})

    Now the collection is empty.

3. Basic Data Types

  • Null
    Indicates a null value or a field that does not exist.
  • Boolean
  • 32-bit integer
    Cannot be expressed in shell. Javascript only supports 64-bit floating point decimals, so it will be converted to 64-bit floating point decimals.
  • 64-bit integer
    Same as above
  • 64-bit floating point Decimals
  • String
  • Symbol
    Shell does not support this type. Data of the symbol type in the database is converted to a string.
  • Object ID
    0 1 2 3 | 4 5 6 | 7 8 | 9 10 11
    Timestamp | machine | PID | Increment
  • Date
  • Regular Expression
  • Code
  • Binary data
  • Maximum Value
    Bson has such a special type to indicate the maximum possible values. Shell does not support this type.
  • Minimum value
  • Undefined
  • Array
  • Embeded document
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.