MongoDB performs CRUD operations on images-combined with JAVA, mongodbcrud

Source: Internet
Author: User
Tags findone install mongodb

MongoDB performs CRUD operations on images-combined with JAVA, mongodbcrud

The previous blog briefly introduced how to install MongoDB, how to operate MongoDB in dos, and how to solve the errors in installing MongoDB. Of course, these are not enough. We need to use them in practice. I used MyEclipse + JDK1.7 to make a simple demo to demonstrate how to use MongoDB in practice.

As a representative of a NoSql database, should MongoDB be a strong point in accessing multimedia data? In MongoDB, how does one perform CRUD operations on images.

As mentioned in previous blogs, the document structure of MongoDB is in the BSON format. The BSON format itself supports storing binary data, therefore, the binary data of the file can be directly stored in the document structure of MongoDB. However, the maximum length of a BSON file cannot exceed 4 MB. Therefore, the maximum size of files stored in a single document cannot exceed 4 MB. To solve this problem, MongoDB provides the "GridFS" mode, "GridFS" Mode for file operations need to introduce the relevant jar package-mongo-java-driver-2.9.3.jar.

Not to mention, look at the instance:

Package com. zd. mongodb. common; import java. io. file; import java. io. IOException; import java.net. unknownHostException; import com. mongodb. DB; import com. mongodb. DBCollection; import com. mongodb. DBObject; import com. mongodb. mongo; import com. mongodb. gridfs. gridFS; import com. mongodb. gridfs. gridFSDBFile; import com. mongodb. gridfs. gridFSInputFile; public class MongoDBCommon {// connect mongodbpublic DB MongoDbConnection () {Mongo m = null; try {m = new Mongo ("localhost", 27017);} catch (UnknownHostException e) {e. printStackTrace ();} DB db = m. getDB ("test"); return db;} // Save the image information public void saveFile (String connection, String id, String file) {DB db = MongoDbConnection (); // obtain the MongoDB database File imageFile = new File (file); GridFS gfsPhoto = new GridFS (db, connection); // connection is the set term GridFSInputFile gfsFile = null; try {gfsFile = gfsPhoto. createFile (imageFile);} catch (IOException e) {e. printStackTrace ();} gfsFile. setId (id); gfsFile. setContentType ("image/jpeg"); gfsFile. save () ;}// read the public void readFile (String connection, String fileName) {DB = MongoDbConnection (); // obtain the connection DBCollection collection = db. getCollection (connection); // connection is the set name String newFileName = fileName; GridFS gfsPhoto = new GridFS (db, connection); GridFSDBFile imageForOutput = gfsPhoto. findOne (newFileName); System. out. println (imageForOutput); // The returned data is in BSON format} // query the corresponding file public void findByObjectId (String connection, String id) {DB db = MongoDbConnection () according to the id (); DBCollection collection = db. getCollection (connection); DBObject dbObj = collection. findOne (id); System. out. println (dbObj); // return data in BSON format} // Delete the public void deleteFile (String connection, String id) {DB db = MongoDbConnection (); DBCollection collection = db. getCollection (connection); DBObject o = collection. findOne (id); collection. remove (o );}}


Client:

Package com. zd. mongodb. common; public class ZhudanTest {public static void main (String [] args) throws Exception {MongoDBCommon dbCommon = new MongoDBCommon (); String newFileName = "zhudan.jpg"; String imageFile = "D: /life/photo/me/zhudan.jpg "; // String connection =" photo. files "; String connection =" photo "; // save data by id String id =" 1111 "; dbCommon. saveFile (connection, id, imageFile); // obtain data by name // dbCommon. readFile (connection, newFileName); // delete data by id // String id = "1111"; // dbCommon. deleteFile (connection, id); // query data by id // String id = "1111"; // dbCommon. findByObjectId (connection, id );}}



Further analysis principle:

When creating photo, the driver will first create two sets in the current database: one is "photo. files "set, and" photo. chunks "set. The former records basic information such as the file name, File Creation Time, and file type. The latter stores binary data of the file in blocks (and supports encryption of these binary data ).

A trunk has a default size. When the size of the file exceeds the default size, the file is split into multiple chunks and then saved to photo. chunks, and finally save the file information to photo. files.

When reading files, first according to the query conditions, in photo. files to find a suitable record, get the value of "_ id", and then send the value to photo. in the chunks, find all the chunks whose "files_id" is "_ id" and sort them by "n". Then, read the content of the "data" object in the chunk in sequence and restore it to the original file.

Speaking of the principle, during the test, when saving images, the Set Name I need is photo. When reading and deleting images, the Complete Set Name I need is photo. files. If the collection name is not photo, no relevant data is found.

Summary:

As a representative of a NoSql database, MongoDB actually contains a lot of things, and I am only exposed to the tip of the iceberg, making a small preparation for learning MongoDB in the future.




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.