Mongodb Guide (translation) (10)-developer zone-Set

Source: Internet
Author: User
Tags mongodb collection mongo shell

The MongoDB set is essentially the name of a group of documents. You can think that they are basically equivalent to tables in relational databases.

Overview

A MongoDB collection is a collection of bson documents. These documents usually have the same structure, but this is not necessary, because MongoDB is a database in free mode (or, more accurately, "dynamic mode. You can store a variety of documents in the same set, and you do not need to pre-define the "column" or field of the set.

 

A collection is created when the first document is inserted.

 

The Set Name should start with a letter or underline and can contain letters; $ is a reserved character. A set can be divided by namespaces. The names of these set groups are defined by "." (points. For example, you can define a collection of blog. Posts and blog. Authors, all of which belong to the "blog" namespace. Note that this is only a mechanism to facilitate user management-from the database perspective, the set namespace is flat.

The Set name can contain a maximum of 128 characters (including the names of databases and indexes ). It is best to control it to 80/90 characters.

Shell

Programmatically, we use the "." flag to access these sets. For example, use Mongo shell:

if( db.blog.posts.findOne() )
print("blog.posts exists and is not empty.");

Other methods to access the collection include:

> db["mycol"].find()
> db.getCollection("mycol").find()

Although underlines are allowed, in shell, if the first character is an underscore, it indicates a special function: Shell considers the prompt to be an actual JavaScript value, rather than a set name. Therefore, "." is not accessible, but can be accessed using getcollection.

> db._mycol.find() --> error
> db.getCollection("_mycol").find() --> success

 

Set of capacity

A set of fixed capacities is a set of fixed sizes that have high-performance automatic-FIFO expiration features (expiration is based on the insertion order ). They are somewhat similar to the concept of "RRD.

In addition, a fixed-capacity set can automatically maintain the insertion sequence of objects in the set at high performance. This is very efficient in some scenarios, such as the log function.

A set of fixed capacities cannot be sharded.

Create

Different from standard sets, you must create a set of capacity explicitly and specify the set size, in bytes. The data space of the set is pre-allocated. The specified size contains the header information of the database.

> db.createCollection("mycoll", {capped:true, size:100000})

Features

  • Once the space is used up, the newly added object will overwrite the old object in the set.
  • If you execute find () without specifying the sorting, the returned objects are always sorted by the insertion order. You can use find (). Sort ({$ natural:-1}) to obtain reverse sorting.

Use and constraints

  • In a set of capacity, you can add new objects.
  • You can update existing objects in the collection. However, these objects cannot add more space. If this is done, the update will fail. Note that if you are performing an update, you can declare an appropriate index (by default, it is determined that the capacity set does not have an index with the "_ id" field ).
  • The database cannot delete objects from a set of capacity. Use the drop () function to delete all rows in the set. (After drop, you must explicitly recreate the set)
  • A set of fixed capacity cannot be sharded.

Application

  • The log-specific capacity set provides a high-performance method for storing log documents to the database. The performance of inserting objects to a set of No-index capacity is very close to that of logging to a file system. In addition, with the built-in FIFO mechanism, you are not at risk of exceeding the disk space in log applications.
  • Buffer. If you want to buffer a small number of objects in the database, or buffer some computing information, a fixed-Capacity Set provides a convenient mechanism to implement it. Note that you need to use indexes on a set of capacity in this application, because the read frequency of this application is higher than the write frequency.
  • Automatic archiving. If you want data to expire automatically, it is more convenient to set the capacity than to write cron scripts.

Suggestions

  • If you can, do not create an index on a set of capacity. If the write frequency of the set is much higher than the read frequency, it is better that there is no index. Note that you can create an index on a set of capacity. However, you will change from "log speed" insertion to "database speed" Insertion-that is to say, compared with the database standard, it is still fast.
  • Use natural ordering to get the latest inserted object more effectively. This is similar to appending data to the end of a log file (tail on a log file.

Optional

Size

The size of a set of capacity must be specified.

Max

You can also set the number of objects in the set. Once this limit is reached, the first inserted object will be removed.

Note: When specifying the number of objects, you must specify the set size. Make sure you have enough storage space to limit the number of objects. Otherwise, the object removal speed will exceed your imagination. You can use the validate () tool to view the space used by the set capacity and estimate the size you need from here.

db.createCollection("mycoll", {capped:true, size:100000, max:100});
db.mycoll.validate();

Autoindexid

The autoindexid field can be set to true or false to enable display or disable automatic creation of unique indexes in the _ id field.

If you use the _ id field, you should create an index in the _ id field.

Because _ id indexes are not used in some cases, it is very helpful to insert data without the _ id field. Most drivers and Mongo shells Add _ id on the client. See how the driver documentation cancels this feature (different drivers may behave differently ). In Mongo shell, you can do this:

> db.mycollection._mongo.insert(db.mycollection._fullName, myObjectWithoutAnId)

Check whether a set has a fixed capacity.

You can use the function iscapped () in the shell to check whether a set has a fixed capacity. DB. Foo. iscapped ()

Converts a set to a fixed-capacity

You can use the converttocapped command to convert a (non-fixed-Capacity) set to a fixed-capacity set:

> db.runCommand({"convertToCapped": "mycoll", size: 100000});
{ "ok": 1 }

Createcollection command

Use the createcollection command to create a collection. It is usually used to create a capacity set.

> # mongo shell
> db.createCollection("mycoll", {capped:true, size:100000})
> show collections

Most drivers also have a helper function to create a set. You can run any command through it.

> db.runCommand( {createCollection:"mycoll", capped:true, size:100000} )

Renamecollection command

This command is used to rename an existing set.

Shell:

> db.oldname.renameCollection("newname")

In the driver, you can use the common command syntax to create:

> db.runCommand( { renameCollection: "mydb.oldname", to: "mydb.newname" }

This command is executed by atoms and should be safe to run on the Production DB. It changes the metadata related to the set and copies the index metadata from the old namespace to the new namespace. The time taken to execute this command is constant and has nothing to do with the size of the set or index. If an open cursor exists on the set during renaming, the cursor becomes invalid and cannot obtain any data.

 

Use a large number of sets

One tips for using mongodb is to use multiple sets of storage information instead of a single set. in this way, some duplicate data no longer needs to be stored in each object, and the index above it can be deleted. More importantly, for performance (based on this issue), data can be aggregated by specified group operations.

For example, assume that we record objects/documents to the database and need M records: One dev record, one debug record, and one ops record. We can store all of them in the same set "logs", similar:

{ log : 'dev', ts : ..., info : ... }

However, if the number of records is not many, it is better to use a set for each log. We can use a "logs. dev" collection, a "logs. debug" collection, and "logs. ops:

// logs.dev:
{ ts : ..., info : ... }

Of course, this makes sense only when we do not need to query multiple types of logs at the same time.

Generally, there are no significant performance defects when using a large number of sets. On the contrary, it will bring good performance.

Restrictions

By default, the set available for each database in mongodb is limited to 24000 namespaces. Each namespace is 628 bytes, And the. ns file is 16 MB by default.

Each set is recorded as a namespace, and each index is also recorded as a namespace. In this way, if each set has an index, we can create a maximum of 12000 sets. The nssize parameter allows you to increase the limit.

Note that each set has a minimum space overhead, Which is kb. Furthermore, any index requires at least 8 KB of data space, because the size of B-tree is 8 KB. If there are a large number of sets, some operations will slow down because the metadata will paged out.

-- Nssize

If you need more sets, specify -- nssize to run MongoDB. This will increase the <database>. Ns file and support more sets. Note -- nssize only affects the new one. NS file -- if you already have a database and want to resize it, run dB in shell after running the database with -- nssize. repairdatabase () to adjust the size.

The maximum size of the. Ns file is 2 GB.

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.