MongoDB Learning Note 01

Source: Internet
Author: User
Tags findone

MongoDB design concept: Can transfer from the server side to the driver to do things, as far as possible to transfer.

Documentation is the core concept of MongoDB. Multiple keys and their associated values are placed together in an orderly manner as documents. Each programming language represents a document in a different way.

The key of the document is a string. Except for a few exceptional cases. The key can use any UTF-8 character. The key cannot contain a (null character), which is used to denote the end of the key; with the special meaning, only in certain circumstances can be used; the keys that begin with the underscore "_" are reserved, although this is not strictly required.

MongoDB distinguishes between type and case. {"Foo": "3"} with {"Foo": 3} {"Foo": 3} and {"Foo": 3}, they are different.

MongoDB cannot have duplicate keys.

A collection is a set of documents. If a document in MongoDB is similar to a row in a relational database, a collection is like a table, and multiple collections can form a database. The collection is modeless, which means that the documents within a collection can be of various kinds. We can identify the collection by name, and the collection name can be any UFT-8 string, as long as the condition is met:

1. The collection name cannot be an empty string

2. The collection name cannot contain the \ s character, which indicates the end of the collection name

3. The collection name cannot be "system." Begins with a prefix reserved for the system collection.

4. User-created collection names cannot contain reserved characters $. Some drivers do support the inclusion of $ in the collection name, because some system-generated collections contain that character. Do not appear in the name unless you want to access this created collection.

Multiple collections can form a database, and the database is identified by its name as a collection, and the name of the database can be any UFT-8 string that satisfies the criteria:

1. Cannot be an empty string

2. Cannot contain "(space) 、.、 $,/, \ and \ n (null characters)

3. All lowercase should be

5. Up to 64 bytes

The database name eventually becomes a file in the file system, which is why there are so many limitations. Put the name of the database in front of the collection, and get the fully qualified name of the collection, called the namespace. The length of the namespace must not exceed 121 bytes and should be less than 100 bytes in actual use.

MongoDB uses port 27017 by default.

Start MongoDB and specify where the data is saved Bin\mongod.exe the path to the--dbpath data store, which is saved by default (C:\DATA\DB)

Inserting data

Reading data

Find returns all the documents in the collection, and if you want to view only one document, you can use FindOne. Find and FindOne can accept qualified conditions in the form of query documents. When you use Find, the shell automatically displays a maximum of 20 matching documents, but you can get more documents.

Update Data Delete data

MONGO is a shell client of MongoDB, is a built-in JavaScript interpreter, and it has some extensions of non-JavaScript syntax.

One technique for understanding function functionality is to not enter parentheses at the input, which will show the JavaScript source code for the function, such as the mechanism or parameters of find:

JavaScript is returned as a collection only if the specified property is not found in db, and can be useful if the property has the same name as the target collection, and you can use the GetCollection function to see the collection of names that contain invalid JavaScript characters.

Objectid uses 12 bytes of storage space, two hexadecimal digits per byte, and is a 24-bit string. This long objectid is twice times as long as the actual stored data. The composition of the Objectid is as follows:

Time stamp accurate to second level

Modifier: $set If the specified key does not exist, create it

$unset Delete the specified key

$inc increase the value of an existing key, or create a key when the key does not exist, can only be used for integers, long integers, or double-precision floating-point numbers

$ne with duplicate data in the $addtoset array is not added

$push if the specified key already exists, an element is added to the end of the existing array, and if not, a new array is created

$each

$pull will erase all matching parts.

$pop Delete the element {$pop: {key:1}} from either end of the array remove {$pop: {key:-1}} from the array header

Positioning modification of an array: Update only the first matching element by position (starting at 0) or positioning operator ("$")

Upsert is a special update that will create a new document based on this condition and update the document if no document meets the update criteria, and if a matching document is found, it is updated normally. Effect

Save is a shell function that can be inserted when a document does not exist, updated when it exists, and has only one parameter: document. If this document contains the "_id" key, save will call Upsert, otherwise it will call insert.

To know how many documents have been updated, you can run the GetLastError command

The values for each key in the Findandmodify command are as follows:

Findandmondify: Collection Name

Query: Querying documents, criteria for retrieving documents

Sort: Criteria for sorting results

Update: Modify the document to perform an update on the found document

Remove: Boolean type that indicates whether the document is deleted

NEW: Boolean type that indicates whether to return the document before the update or the updated document, which is the default before the update

The "Update" and "remove" must have one and only one, and if the document is not matched, the command returns an error. The findandmondify speed is slower than the normal update.

The operations in MongoDB are instantaneous, because they do not have to wait for the database to respond, this is not an asynchronous operation, and the client is doing something else immediately after the document is sent to the server.

MongoDB Learning Note 01

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.