Mongo document file and gridfs file

Currently there is a requirement that there is an "application" object that has: "name", "uploadTime" and "executeFile".

The database is currently mongodb. I want to use a document

Class Application {
     String id;
     String name;
     String type;
     Date uploadTime;
     GridFSDBFile executeFile;
}

      

Dao level, when I write save, I find that I cannot directly link the GridFSDBfile to the document. I need to first use the GridFsTemplate.store to save the GridDBFile and then use the GridFsTemplate.findOne to find it by name and place the object nested in the application document. Then use MongoOperation to save the application document.

try {
        inputStream = new FileInputStream("C:/testing.app");
        gridFsOperations.store(inputStream, "test.app", "jar");

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    Query query = query(where("filename").is("test.app"));
    GridFSDBFile gridFsDBFile = gridFsOperations.findOne(query);

    Application application = new Application ();
    application.setName( "test-app");
    application.setType( "type", "Java");
    application.setexecuteFile (gridFsDBFile);

    applicationDao.save(application);

      

Is it possible to just call MongoOperation? If not, I think the call to GridFsTemplate.store and MongoOperation.save needs to be done in a transaction. I remember Mongo didn't support the transaction. How to do it? Validate transaction in Java code only?

+3


source to share


1 answer


I find that I cannot directly link the GridFSDBfile to the document. I need to first use GridFsTemplate.store to save the GridDBFile, then use GridFsTemplate.findOne to find it by name and place the object nested in the application document. Then use MongoOperation to save the app. document.

You are wrong here. When you save a file to GridFS

, the file storage and its metadata are shared between two collections, namely fs.files

and fs.chunks

. Whenever a file is inserted with GridFs API

, the file and its metadata are saved in one shot.

To improve the image,

When the following statement is executed:

    gridFsOperations.store(inputStream, "test.app", "jar");

      

MongoDB

makes two entries, one in fs.files

and the other in the collection fs.chunks

.

one entry is put into the collection fs.files

:

{
        "_id" : ObjectId("545d6699756c779b8cb8b13d"),
        "type":"jar",
        "filename" : "test.app",
        "aliases" : null,
        "chunkSize" : NumberLong(262144),
        "uploadDate" : ISODate("2014-11-08T00:40:57.298Z"),
        "length" : NumberLong(433),
        "contentType" : null,
        "md5" : "5e49ad0614687c7ad343f6f9fe6843b2"
}

      

This record only contains file metadata information such as file name and type. You can even set other metadata information using the class BasicDBObject

. Do you want to enable uploadTime

,

DBObject metadata = new BasicDBObject();
metadata.put("uploadTime", Calendar.getInstance().getTime());

      



and save it like below,

gridFsOperations.store(inputStream, "test.app", "jar",metadata );

      

It also contains one important information, that is, a link to the document containing the actual content of the file in the field _id

.

fs.chunks

Added another entry to the collection :

{
        "_id" : ObjectId("545d6699756c779b8cb8b13e"),
        "files_id" : ObjectId("545d6699756c779b8cb8b13d"),
        "n" : 0,
        "data" : BinData(0,"PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4N
Cjx4c2w6c3R5bGVzaGVldCB4bWxuczp4c2w9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvWFNML1RyYW5z
Zm9ybSINCnZlcnNpb249IjEuMCI+DQoNCjx4c2w6b3V0cHV0IGVuY29kaW5nPSJVVEYtOCIgbWV0aG9k
PSJ4bWwiIGluZGVudD0ieWVzIi8+DQo8eHNsOnZhcmlhYmxlIG5hbWU9InRvcExldmVsIj4NCiAgICAg
ICAgPHhzbDp2YXJpYWJsZSBuYW1lPSJpbm5lciIgc2VsZWN0PSInSGknIiAvPg0KICAgICAgICA8eHNs
OnZhbHVlLW9mIHNlbGVjdD0iJGlubmVyIi8+DQo8L3hzbDp2YXJpYWJsZT4NCiAgICANCjx4c2w6dGVt
cGxhdGUgbWF0Y2g9Ii8iID4NCiAgICA8eHNsOnZhbHVlLW9mIHNlbGVjdD0iJHRvcExldmVsIiAvPg0K
PC94c2w6dGVtcGxhdGU+DQo8L3hzbDpzdHlsZXNoZWV0Pg==")
} 

      

This is the actual document containing the contents of the file in the form Binary

. Pay attention to the document margin files_id

. It has the same ObjectId

as the links from the collection fs.files

.

This is how the MongoDB

relationship between the content of a file and its metadata is maintained. GridFS

- a simple API that allows you to abstract the user from low-level processing.

f no, I think the call to GridFsTemplate.store and MongoOperation.save needs to be done in a transaction. I remember Mongo does not support the deal. How to do it? Validate transaction in Java code only?

The application does not need to create or maintain any transactions for this operation.

+2


source







All Articles