Mongodb stores and reads Word documents and mongodbword documents

Source: Internet
Author: User

Mongodb stores and reads Word documents and mongodbword documents

To ensure that printed letters can be restored, You need to record the content of Word letters every time you print a letter.

SQL Server can only record the text content of a letter. How can I store the page layout, font format, and other content of a letter? At this time, Mongodb debuted. Because MongoDB's document structure is in BJSON format (BJSON Full name: Binary JSON), BJSON itself supports storing data in Binary format, therefore, the binary data of the file can be directly stored in the document structure of MongoDB. The file is then retrieved in binary format, so that the file can be saved lossless.

Below is what I have verified. I will store the Word to Mongo and then read the Word code from Mongo. I will share with you here.

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. IO; using MongoDB. bson; using MongoDB. driver; namespace Mongodb {public partial class Form1: Form {public Form1 () {InitializeComponent (); Init () ;}// database connection string const string strconn = "mongodb: // 127.0.0.1: 27017 "; // database name const string dbName =" test "; Login server; login database db; void Init () {// create database link server = MongoDB. driver. login server. create (strconn); // obtain database db = server. getDatabase (dbName);} private void btnSave_Click (object sender, EventArgs e) {SaveDocToMongo (@ "d: \ quwenzhe.docx");} private void btnShow_Click (object sender, EventArgs e) {GetDocFromMongo (@ "E: \ newquwenzhe.doc ");} /// <summary> /// Save the Word to Mongo /// </summary> /// <param name = "filename"> file name to be saved </param> private void SaveDocToMongo (string filename) {byte [] byteDoc = File. readAllBytes (filename); BsonDocument doc = new BsonDocument (); doc ["id"] = "1"; doc ["content"] = byteDoc; your collection col = db. getCollection ("doc"); col. save (doc );} /// <summary> /// Save the Word in Mongo to the local device // </summary> /// <param name = "filename"> Save the Word to the local file name </param> private void GetDocFromMongo (string filename) {collections collection col = db. getCollection ("doc"); var query = new QueryDocument {"id", "1" }}; var result = col. findAs <BsonDocument> (query); byte [] buff = (byte []) (BsonDocument) result. toList () [0]). getValue ("content"); FileStream fs; FileInfo fi = new FileInfo (filename); fs = fi. openWrite (); fs. write (buff, 0, buff. length); fs. close ();}}}
After the storage operation is completed, you can view the binary data in the stored Vue, as shown in:

By now, the weak source code is: http://pan.baidu.com/s/1pj5dter.

Okay, it's not too early. I have to take a nap and prepare for the soft exam in the afternoon. Thank you for watching it.


Would NoSql like MongoDB be able to store only text information but not binary information?

MongoDB is a document-based storage database. Its storage is provided to the file storage system in the operating system. Therefore, mongodb can be stored as long as it is available in the file system.

If you want to save some binary big data files, you can use the GridFS data structure.

An exception occurred in the Gridfs storage file of Mongodb.

This is because the domain name in the same system is changed or the same domain name is used in different systems, resulting in access errors for different instances:
After changing the domain name, restart all MongoDB/mongos instances.

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.