MongoDB Learning Note (i): Installation and simple shell operation

Source: Internet
Author: User
Tags arrays documentation json mongodb mongodb version naming convention regular expression reserved

I. Description

1, the study of the series MongoDB learning environment using the MongoDB version of mongodb-win32-i386-2.4.6, operating system for Win7.

Second, installation

1, new two directories, respectively, D:\Installations\MongoDB-2.4.6\MongoDB and D:\Installations\MongoDB-2.4.6\MongoDBDATA (using Mongod to start the service, You need to specify a data directory to hold the database data file, which is C:\data\db by default under Windows).

2, the download of the MongoDB compression package decompression, and copy the files inside the D:\Installations\MongoDB-2.4.6\MongoDB directory, the directory structure is as follows:

3. Configure the Bin directory to the system environment variable.

4. Install the MongoDB database and start the database service: The following diagram installs the MongoDB database and starts the service (the data directory is specified by--dbpath display), and later launches the database, using the same command. You can find that when you start the database service, MongoDB listens on port 27017 by default, and automatically starts an HTTP service with a listening port of 28017, and you can login to the Web interface by entering http://localhost:28017 in the browser.

5, MongoDB also provides the form of a service to start the database, as follows: Need to specify a log file path through--logpath


third, MongoDB and the traditional relational database

1. Documentation

It is a basic unit of data in MongoDB, similar to a row in a relational database, where multiple key-value pairs are placed in an orderly fashion. The distinction between documents in MongoDB includes: The order of key-value pairs, the type of keys and values, and the case of keys and values. In two documents, if there is any inconsistency between the three, the two documents will be different. The data type of the value of a key-value pair in a document can even be an entire embedded document. The "key" is only a string and the naming convention is:

The key cannot contain "\" (null character), which is used to denote the end of the key
The character "$" has a special meaning and in general avoids the use of
Names that begin with an "_" underscore are reserved for the system, and avoid using a "key" to represent a field that represents a table in a traditional relational database. 2. Collections in MongoDB represent a set of documents, similar to tables in a relational database. But the tables (collections) in MongoDB are not modal and can put completely different documents into the same collection, but it is not supported in terms of database management, operational efficiency, building indexes, or the best practice is to implicitly prescribe a pattern for a particular set. The naming conventions for collections are:
Cannot be an empty string ""
Cannot contain "\" (null character), which is the flag of the end of the collection name
Cannot use "system." At the beginning, this is the system reserved prefix
The name cannot contain "$", this character is used for system collections
One Convention for organizing collections is through the "." A subset of characters separated by namespace, such as User.class, the collection user (which may not exist) and this sub-set has no relation
3, a set of databases can form a database. A MongoDB instance can host multiple databases. Each database has a separate permission control. The naming conventions are:
Cannot be an empty string ""
Cannot contain "(space) 、.、 $, \,/and (null characters)
should be all lowercase and up to 64 bytes
The database name will eventually become the file system file name. There are special databases in the MONGODB system that are pre-built, and these names are not available:
Admin: From the perspective of permissions, this is the "root" database, the user in this database will inherit all the permissions of the database, some specific server commands, such as listing all databases, shutting down the server and so on can only be executed from this database. Local: Data in this database is not replicated and can be used to store any collection of local single servers
Config: When MONGO is used for sharding settings, the database is used internally to hold information about the Shard.
Placing the database name in front of the collection name is the fully qualified name of the collection, called the namespace. The fully qualified name must not exceed 121 bytes in length.

4. The number, name, and type of keys in each document of a collection in MongoDB can be different.

Iv. MongoDB Data type

JSON supports only: null, Boolean, numeric, string, array, and object in terms of data types. And JSON itself does not directly support the date type, and for numbers, it is not possible to distinguish between integers and floating-point numbers, not to distinguish between 32-digit and 64-digit digits. MongoDB uses the Bson data format for data transfer and storage, which is an extension of JSON, adding data types such as dates, floating-point numbers, etc. that are not supported by JSON. As follows:

Null: Used to represent a null or nonexistent field
Boolean
Number: The number in the shell is 64-bit floating-point numbers, so the document that is inserted into the collection through the shell contains 64-bit floating-point numbers. If you insert a document in another way into the collection that contains a 32-bit integer, the document is updated by the shell, and the number is updated to 64-bit floating-point numbers even if you do not update the key-value pair for the 32-bit integer. Because a 64-bit floating-point number can accurately represent a 32-bit integer, there is no problem with the display. However, a 64-bit floating-point number indicates that a 64-bit integer is sometimes problematic, and when you view a 64-bit integer in the shell, it is rendered as a built-in document, as shown in the following illustration: Built-in documentation if only a key such as "Floatapprox" is present, this 64-bit integer can be To accurately represent a 64-bit floating-point number, the shell-displayed built-in document adds two additional key "top" and "bottom" to the built-in document, representing a high 32-bit and a low 32-bit string, respectively.
Date: The number of milliseconds stored in the data store, starting from the standard era, with no time zone information stored
Not defined: undefined
Array: An array is a set of values that can be represented either as ordered objects (lists, stacks, queues, etc.) or as unordered objects (collections). Arrays can contain elements of different data types, and all value types supported by regular key-value pairs can be placed in arrays. Arrays can also be nested in an array. The array in the document has an attribute: MongoDB can go deep inside the array to search.
Inline document: A value that corresponds to the entire document as a key of another document. This organizes the data so that it does not have to be a flat structure. Like an array, MongoDB understands the structure of the built-in document and delves into it to build indexes, execute queries, and so on.
Regular Expressions: You can include regular expressions in your document, using the regular expression syntax of JavaScript.
The document stored in object Id:mongodb must have a key of "_id", its value can be any type, but it must be guaranteed that its value is unique within a collection, and that the same document "_id" can exist in multiple collections. If you do not indicate "_id" when you insert a document into the collection, the system automatically adds the key to the document, and its value defaults to the Objectid object. Objectid is the default value type for "_id", and different machines can use the same mechanism to generate different primary keys. MongoDB does not use self-growth as the most commonly used primary key strategy because it is inherently oriented to distributed databases, and it is time-consuming to synchronize self-growing primary key values among a large number of hosts, and to be laborious and error prone. The Objectid uses 12 bytes of storage space, divided into four groups, generated in this way: {0,1,2,3}| {4,5,6}| {7,8}| {9,10,11}, which are timestamps, machines, PID, and counters, respectively. This is sufficient to guarantee the uniqueness of this value. The best practice is to add this key when inserting a document and ensure that its value is unique within the collection. This can reduce the cost of the database side, and also embodies a design concept: can be transferred from the database layer to the application layer operation is transferred out, after all, the expansion of the application layer is easier than the expansion of the database layer. If this is done, after inserting the document can also return the document "_id" directly to the client, if through MongoDB to generate this value, after inserting the document will need to find again before you can get this value.

Five, MongoDB shell command line and basic use

1. MongoDB comes with a JavaScript Shell, which is a JavaScript interpreter or a MONGODB client that can interact with the launched DB instance through JavaScript (the shell commands are case sensitive). In the shell, the shell returns the result whenever a complete JS code is written.

2, start the database service can login Shell command line: The following figure, the default is to log on to the test database, if you want to log into the admin database, you can use the command: MONGO 127.0.0.1:27017/admin. You can also use only command MONGO to connect to the test database of the local database server. Once connected, the database connection is assigned to the global variable db.

3. View all databases: Show DBS.

4. Create a switch between database and database: Use database name, using this command can switch between different databases (switch global variable DB currently pointing to the database); When the database to be switched does not exist, the database of that name is created, but the database is not actually created. It is only in the MongoDB cache, that is, in the pre-processing buffer pool has the database, if you do nothing to leave, the database will be automatically deleted by MongoDB, when the database is added to the collection will not be really created.

5. View all the collections in a database: Show collections. If there is a collection that already exists in the database and the document is inserted in the collection, then using that command to view the collection will find a System.indexes collection that is responsible for storing the index, because without a key called "_id" when inserting a document, Then a "_id" key is added automatically, and a unique index is created for the key by default, so a collection of system.indexes is added.

6. Delete the specified collection in the database: DB. Collection name. Drop (). The DB here is the database that represents the current entry, using the. Collection name to get a collection in the database, and using the db["collection name" to get a collection, especially if the collection name contains certain special characters, use "." is most likely not available, and is particularly effective when using []; In addition to getting a collection in both ways, you can also use function getcollection, such as Db.getcollection ("collection name"). Drop (). And the collection is created the first time it is used.

7. Delete the current database: Db.dropdatabase ().

8. The Help function in the shell: How do you know what you can do when you go into a database? At this point, you can use the Help function, as shown below, to list the usage of the database level, of course, in addition to the database-level help, there is a collection-level helps, using the method: Db. Collection name. Help (). You can also view the source code of a function without adding "()" after the function name.

8, the shell built-in JS engine can directly execute the execution of JS code, as follows

If you start the shell just to experiment with JavaScript, the shell that runs MONGO--nodb does not connect to any database.

9. Although the global variable db provided in the shell points to the currently connected database, other variables can be used to hold other connected databases, using the Connect () command provided in the shell, as shown below:


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.