문제

I insert data to GridFS using the following command line:

mongofiles --host localhost:27017 --db testmongo --collection files put D:/text.txt

It has fields like _id filename chunkSize uploadDate md5 length.

How can I add my own fields using cmd or Java?

도움이 되었습니까?

해결책

There is no way to do this using the mongofiles command line utility, but it's straightforward using the Java driver:

    MongoClient client = new MongoClient();
    GridFS gridFS = new GridFS(client.getDB("test");
    GridFSInputFile in = gridFS.createFile(<insert bytes here>);
    in.put("meta", 5);  // insert extra metadata here
    in.save();
    GridFSDBFile out = gridFS.findOne( new BasicDBObject( "_id" , in.getId() ) );
    System.out.println(out.get("meta"));  // this will print 5

Basically, just put the metadata on the GridFSInputFile, and get it from the GridFSDBFile.

The file will look like this (using the shell):

> db.fs.files.findOne()
{
    "_id" : ObjectId("5333184bb0c659a378532bda"),
    "chunkSize" : NumberLong(261120),
    "length" : NumberLong(3),
    "md5" : "acbd18db4cc2f85cedef654fccc4a4d8",
    "filename" : null,
    "contentType" : null,
    "uploadDate" : ISODate("2014-03-26T18:11:23.973Z"),
    "aliases" : null,
    "meta" : 5
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top