1. Tips for using shell
Shell built-in help:
You can use dB. Help () to view database-level command help. You can use dB. Blog. Help ()
Do not enter parentheses when entering the function, so that the JavaScript source code of the function can be displayed:
Using the DB. Set Name method to access the set is generally normal, but if the set name happens to be an attribute of the Database Class, this will cause problems, for example:
To access the version set, you cannot use dB. version because dB. version is a database function.
When a specified property is not found in the DB, JavaScript returns the property as a set. If the property has the same name as the target set, you can use
Getcollection function, such as DB. getcollection ("version ")
To view the set of invalid JavaScript characters in the name, for example, DB. Blog-refactor is a valid set name, but it becomes
Variable subtraction. You can get the blog-refactor set through dB. getcollection ("blog-refactor.
In JavaScript, x. y and X ['y'] are equivalent.
VaR collections = {"posts", "Comments", "Authors "};
For (I in collections)
{
Dostuff (db. Blog [collections [I])
}
Instead of writing it like this:
Dostuff (db. Blog. Posts)
Dostuff (db. Blog. Comments)
Dostuff (db. Blog. Authors)
2. Data Type
Basic Data Types
MongoDB documentation is similar to JSON. It is similar in concept to JavaScript objects.
MongoDB adds other data types based on the Basic JSON key-value pairs.
1. null
Indicates a null value or a field that does not exist.
{"X": NULL}
2. Boolean Value
True or false
{"X": false}
An integer of 3.32 digits.
This type is not applicable in shell, because JavaScript only supports 64-bit floating point numbers, so 32-bit integers will be automatically converted
An integer of 4.64 digits.
This type is not applicable in shell. Shell uses a special embedded document to display 64-bit integers.
5.64-bit floating point number
The numbers in shell are of this type, and the following are all floating point numbers.
{& Quot; X & quot;: 3.14}
{"X": 3}
6. String
UTF-8 strings can all be represented as strings.
{"X": "refactor "}
7. Symbol
This type is not applicable in shell. Shell converts the symbol type in the database to a string
8. Object ID
The object ID is the unique ID of 12 bytes of the document.
{"X": objectid ()}
9. Date
The date type stores the number of milliseconds starting from the standard epoch. the time zone is not stored.
{"X": new date ()}
10. Regular Expression
The document can contain regular expressions, using JavaScript syntax
{"X":/refactor/I}
11. Code
The document can contain JavaScript code
{"X": function (){....}}
12. binary data
Binary data is composed of any byte string and cannot be used in shell.
13. Maximum Value
The special type contained by bson, which indicates the maximum possible value. It cannot be used in shell.
14. Minimum value
The special type contained by bson, indicating the possible minimum value, which cannot be used in shell.
15. undefined
Undefined types can be used in this document
{"X": Undefined}
16. Array
A set or list of values can be expressed as an array.
{"X": ["A", "B", "C"]}
17. embedded document
The document can contain other documents or be embedded into the parent document as values.
{"X": {"hello": "world "}}
Numeric type
There is only one "Number" type in Javascript. MongoDB has three numeric types (32-bit integer, 64-bit integer, 64-bit floating point number ),
Shell must bypass Javascript restrictions. By default, all the numbers in shell are treated as double-precision data by MongoDB, which means
If you get a 32-bit integer from the database, after modifying the document and saving the document back to the database, the integer is converted
Floating Point number. If this integer is not modified, it is converted to a floating point number. Therefore, it is not necessary to overwrite the entire document under the sub-shell.
A number can only be expressed as a double-precision number (64-bit floating-point number). Another problem is that some 64-bit integers cannot be expressed as 64-bit floating-point numbers.
A 64-bit integer. It can be viewed in shell and expressed as an embedded document, which may be inaccurate.
Date type
In JavaScript, the date object is used as the date type of MongoDB. To create a new date object, new date () is usually called instead of date ()
Calling Constructors (excluding New) actually returns the string representation of the date, rather than the real date object, which is a feature of JavaScript itself.
The date display in Shell uses the local time zone settings. However, the date is stored in milliseconds starting from the standard epoch, and no time zone information is related to it.
Array type
An array is a set of values that can be operated as an ordered object (such as a list, stack, or Queue) or as an unordered object (such as a set.
E. g.: {"things": ["pie", 3.14]}
Arrays can contain elements of different data types (in this example, a string and a floating point number). In fact, the values supported by common key-value pairs can be
As an array element, or even a nested Array
MongoDB can query 3.14 of all documents in the "things" array. If this query is frequently used, you can create indexes for "things" to improve performance.
MongoDB can use atomic updates to modify the content in the array. For example, you can go deep into the array and change pie to pi.
Embedded Document Type
Embedded documents use the entire MongoDB document as a key value in another document. This makes the data more organized and does not have to be stored in a flat structure.
For example, a document should be used to indicate a person and save the address. You can embed the address into the document.
Peopleaddress =
{
"Name": "refactor ",
"Address ":
{
"Street": "wuzhong ",
"City": "Suzhou"
}
}
Like arrays, MongoDB can understand the structure of embedded documents, build indexes in depth, and perform queries or updates.
Embedded documents can change the way data is processed. In relational databases, previous documents are generally split into two tables ("people" and "Address ")
In MongoDB, the address document can be directly embedded into the personnel document. This also has a disadvantage, because MongoDB will store a lot
If "Address" is in an independent table in a relational database, you need to modify the spelling of the address.
After modification, the information of every person using this address will be updated. However, in MongoDB, You need to modify spelling errors in each person's documents.
_ Id and objectid
The document stored in MongoDB must have a "_ id" key. The value of this key can be of any type. The default value is the objectid object.
Each document in a set has a unique "_ id" value to ensure that each document in the SET has a unique identifier.
Two sets can have a "_ id" key with a value of 123, but each set can have only one "_ id" 123 document.
Objectid is the default type of "_ id". objectid ensures the uniqueness of distributed data.
If you do not have the "_ id" key when inserting a document, the system will automatically create one for you. This can be done by the MongoDB server,
But it is usually completed in the client driver for the following reasons:
1. Although objectid is designed to be lightweight and easy to generate, after all, it still produces overhead during generation, which is reflected in client generation.
MongoDB Design Philosophy: migrate from the server to the driver as much as possible. The reason for this idea is: expand the application layer ratio.
The extended database layer is much easier, and the transaction is handed over to the client for processing, which reduces the burden on database expansion.
2. Generate objectid on the client, and the driver can provide richer APIs. For example, the driver can have its own insert method and can return the generated objectid.
You can also insert the objectid directly into the document. If the driver allows the server to generate objectid, You need to query it separately to determine
The value of "_ id" in the inserted document.