Ruby Operations MongoDB Database (Advanced 12)--gridfs

Source: Internet
Author: User

In this chapter, we carry out the last article of the Advanced Tutorial, GRIDFS study. In this chapter, we will learn the following:

Create a Gridfs object ("Grid::fsbucket")

    • Write Data stream Processing

    • Read Data stream Processing

    • Find the file meta data

    • File deletion

    • Manipulating Grid::file objects

    • File insertion

    • File Lookup

    • File deletion

The Ruby driver provides a simple and neat interface for the processing of block file storage in a database, the "Gridfs". This API interface allows us to use both the Grid::file object and the read-write stream.

    1. Create a Gridfs object ("Grid::fsbucket")

On the database, you can create a Gridfs object by calling FS and providing a series of optional parameters, returning a Grid:fsbucket object. The optional parameters are shown in the following table:

Parameters Parameter description
: Bucket_name Default Gridfs bucket Name
: Fs_name Gridfs bucket name, covering in the FS function: Bucket_name
: chunk_size Set the size of each tile file in the database
: Write Write parameters to use when uploading files
: Read Preferences for using read operations when asking for price downloads
For example, you can set a specific read operation preference for the created Gridfs:bucket:
Fs_bucket=database.fs (Read:{mode:secondary})

2. Write Data Flow operations

By writing the data to the Gridfs upload a file, you can open a stream port and then write directly to the data, you can also combine all the content into an Io object together to write Gridfs.

2.1 Open an upload data stream and write the data

File=file.open ('/path/to/my-file.txt ', ' R ') fs_bucket.open_upload_stream (' My-file.txt ') do |stream| Stream.Write (file) endfile.close

2.2 Write once by synthesizing all content into an Io object

Flie=file.open ('/path/to/my-file.txt ', ' R ') fs_bucket.upload_from_stream (' My-file.txt ', File) file.close

3 Read Data Flow operations

Using a read stream to download a file from Gridfs, you can read it directly after opening a stream of data, or you can complete a document download in one step.

3.1 First open a download data stream and read it directly

File=file.open ('/path/to/my-output-file.txt ', ' W ') Fs_bucket.open_dowmload_stream (file_id) do |stream| File.write (Stream.read) endfile.close

3.2 Directly after downloading the file directly to the IO object

File=file.open ('/path/to/my-output-file.txt ', ' W ') Fs_bucket.download_from_stream (file_id,file) file.close

You can also download the file by a specific name and the corresponding version number (optional parameters). Revision numbers are sorted by upload date for file differentiation between files of the same name. The revision number passed to the Open_download_stream_by_name method can be positive or negative.

File=file.open ('/path/to/my-output-file.txt ', ' W ') fs_bucket.open_download_stream_by_name (' My-file.txt ', revision   :-2) do |stream| File.write (stream.read) End

Download all the contents of a document by document name and revision number (optional)

File=file.open (' Path/to/my-output-file.txt ', ' W ') fs_bucket.download_to_stream_by_name (' My-file.txt ', revision:-2 ) File.close

4 Finding document metadata

You can retrieve documents containing metadata in the Gridfs document collection

Fs_bucket.find (filename: ' My-file.txt ')

5. deleting files

You can delete files by ID

Fs_bucket.delete (FILE_ID)

6. Using the Grid::file object

This object can be used to wrap a file to be inserted into a database, and the object can also be retrieved Gridfs. Create a file that contains data.

File=mongo::grid::file.new (' I am a File ',:filename=> ' new-file.txt ')

Create a Ruby File object:

File=file.open ('/path/to/my-file.txt ') grid_file=mongo::grid::file.new (File.read,:filename=>file.basename ( File.path))

Change file parameters, such as block size, and pass corresponding parameters to the constructor

File=file.open ('/path/to/my-file.txt ') grid_file=mongo::client.file.new (File.read,: Filename=>File.basename (File.path),: chunk_size-=>1024)

The optional parameters supported by file operations are listed in the following table

Parameters Parameter description
: Chunk_sie Set the size of each file block in the database
: Content_Type Set the content type of the file
: filename (Required) Filename
: Upload_date Date the document was uploaded (stored)

7. File Insertion

You can insert one document to the database at a time. The block file is inserted into the Fs.chunks collection by default, and the file metadata is inserted into the Fs.files collection.

Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' film ') file=mongo::grid::file.new (' I am a file ',: Filename=> ' New-file.txt ') client.database.fs.insert_one (file)

Use the name prefix instead of the FS function, using the: Fs_name parameter to access the file system

Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' film ') file=mongo::grid::file.new (' I am a file ',: Filename=> ' New-file.txt ') client.database.fs (:fs_name=> ' grid '). Insert_one (file)

Note that the first time the file is inserted, the system automatically creates an index on the Block collection. A composite index is created

{: files_id=>1,:n=>1}

The same file can be streamed and inserted directly

Client.database.fs.open_stream (filename) do |stream| Stream.Write (file) end

8. File Search

Retrieving a file from the database can be achieved by using the appropriate filtering rules in the Find_one

Client=mongo::client.new ([' 127.0.0.1:27017 '],:d atabase=> ' film ') Client.database.fs.find_one (:filename=> ' New-file.txt ')

You can also find by streaming operations

Client.database.fs.open_download_stream (file_id) do |stream| Io.write (stream.read) End Fs.download_to_stream (File_id,io)

9. File deletion

Delete a file using the Delete_one method of the object

Client=mongo::client.new (' 127.0.0.1:27017 ',:d atabase=> ' film ') Fs=client.database.fsfile=fs.find_one (: Filename=> ' My-file.txt ') fs.delete_one (file)

At this point, we completed the Gridfs learning and related operations.

This article from "Techfuture" blog, declined reprint!

Ruby Operations MongoDB Database (Advanced 12)--gridfs

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.