【发布时间】:2014-04-28 14:30:33
【问题描述】:
我使用以下命令行向 GridFS 插入数据:
mongofiles --host localhost:27017 --db testmongo --collection files put D:/text.txt
它有像 _id 文件名 chunkSize uploadDate md5 长度这样的字段。
如何使用 cmd 或 Java 添加自己的字段?
【问题讨论】:
我使用以下命令行向 GridFS 插入数据:
mongofiles --host localhost:27017 --db testmongo --collection files put D:/text.txt
它有像 _id 文件名 chunkSize uploadDate md5 长度这样的字段。
如何使用 cmd 或 Java 添加自己的字段?
【问题讨论】:
使用 mongofiles 命令行实用程序无法做到这一点,但使用 Java 驱动程序很简单:
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
基本上,只需将元数据放在 GridFSInputFile 上,然后从 GridFSDBFile 中获取即可。
文件将如下所示(使用 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
}
【讨论】: