Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MongoDB practice (3) fixed set and GridFS

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

1. Fixed set (Capped Collection)

Capped collections is a high-performance collection with a fixed size, age-out (aging removal) processing with LRU (least recently used by Least Recently Used) rules and insertion order, automatically maintains the insertion order of objects in the collection, and specifies the size in advance when creating. If you run out of space, the newly added objects will replace the oldest objects in the collection.

You can insert and update, but the update cannot exceed the size of the collection, otherwise the update fails. Deletion is not allowed, but you can call drop () to delete all rows in the collection, but you need to explicitly rebuild the collection after drop.

Common uses:

1 、 logging

The first choice of logging mechanism in MongoDB, MongoDB does not use log files, but stores log events in the number of

According to the library. The speed of inserting an object in a capped collection without an index is the same as the date recorded in the file system

Zhi's speed is about the same.

2 、 cache

Cache some objects in the database, such as calculated statistics. This needs to be established on collection.

An index, because using the cache tends to read more than write.

3 、 auto archiving

You can take advantage of the age-out feature of capped collection, eliminating the need to write cron scripts for manual archiving.

Recommended usage:

1. In order to maximize the performance of capped collection, if there are more writes than reads, it is best not to build an index on it, otherwise

Insert speed decreased from "log speed" to "database speed".

2. Using "nature ordering" can effectively retrieve the most recently inserted element, because capped collection can guarantee

The natural sort is the order in which it is inserted, similar to the tail operation on the log file.

Actual case:

You can specify the maximum number of documents that can be stored in the collection when you create the capped collection. But at this time it also means

Set size, because always check size first and then check maxRowNumber.

You can use validate () to see how much space a collection has used to determine how much size is set.

Let's create a collection:

Db.createCollection ('mycappc1', {capped:true,size:100000,max:10})

This is a fixed collection of up to 10 rows of records.

When we insert 10 records and a new one is inserted, the oldest one will be removed and see the following effect:

Check how much space to use:

The above-mentioned createCollection function can also be used to create a similar collection, with a parameter.

"autoIndexID", the values can be "true" and "false" to determine whether the index needs to be automatically created on the "_ id" field.

As follows:

Db.createCollection ('mycappc2', {capped:true,size:100000,max:10,autoIndexId:false})

Then the table has no index, which is very suitable for tables that write more and read less.

II. GridFS

GridFS is a file specification that stores large files in an MongoDB database.

Because the size of BSON objects in MongoDB is limited, the GridFS specification provides a transparent mechanism that can

To split a large file into several smaller documents, this mechanism allows us to effectively save large file objects

Especially for those huge files, such as videos, high-definition pictures and so on.

GridFS uses two tables to store data:

Files contains metadata objects

Chunks binary blocks that contain some other relevant information

In order for multiple GridFS to be named as a single database, files and blocks have a prefix, and by default, the prefix

Is fs, so any default GridFS storage will include the namespaces fs.files and fs.chunks. A variety of third-party languages

The driver has permission to change this prefix, so you can try to set another GridFS namespace to store photos, which

The specific locations are: photos.files and photos.chunks. Let's take a look at a practical example.

Command line tools are used here

/ usr/bin/mongofiles put / tmp/testfile# results as follows: connected to: 127.0.0.1added file: {_ id: ObjectId ('5280bfc58bff9a82c5ba2abc4'), filename: "/ tmp/testfile", chunkSize: 262144, uploadDate: new Date (1384169413607), md5: "21395b76d80f48cc069fa90d0c639513", length: 29} thanks # View / usr/bin/mongofiles listconnected to: 127.0.0.1/tmp/testfile 29

Next, let's go into the warehouse to see if there is anything new.

Field description:

Filename: stored file name

ChunkSize: the size of the chunks part

UploadDate: storage time

Md5: the md5 code of this file

Length: file size in bytes

It seems that some basic metadata information is stored in fs.files.

The more important field is "n", which represents the serial number of chunks, which starts from 0, so it seems that fs.chunks

Some actual content data information is stored in.

Now that we can save this file, we should have a way to take it out. Let's take a look at the example:

-check md5, and the result is the same as that in the library.

Db.fs.chunks.ensureIndex ({files_id:1, n unique 1}, {unique: true})

In this way, a block can be retrieved using its files_id and n values. Note that GridFS can still use findOne

Get the first block, as follows:

Db.fs.chunks.findOne ({files_id: myFileID, n: 0})

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report