3) 输出已保存的所有图片
下面代码段,输出所有保存在photo命名空间下的图片信息:
GridFS gfsPhoto =new GridFS(db, "photo");
DBCursor cursor = gfsPhoto.getFileList();
while(cursor.hasNext()){
System.out.println(cursor.next());
}
DBCursor cursor = gfsPhoto.getFileList();
while(cursor.hasNext()){
System.out.println(cursor.next());
}
4) 从数据库中读取一张图片并另存
下面的代码段,从数据库中读取一张图片并另存为另外一张图片到磁盘中
String newFileName ="mkyong-java-image";
GridFS gfsPhoto =new GridFS(db, "photo");
GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName);
imageForOutput.writeTo("c:\\JavaWebHostingNew.png");
GridFS gfsPhoto =new GridFS(db, "photo");
GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName);
imageForOutput.writeTo("c:\\JavaWebHostingNew.png");
5) 删除图片
String newFileName ="mkyong-java-image";
GridFS gfsPhoto =new GridFS(db, "photo");
gfsPhoto.remove(gfsPhoto.findOne(newFileName));
GridFS gfsPhoto =new GridFS(db, "photo");
gfsPhoto.remove(gfsPhoto.findOne(newFileName));
如何将JSON数据格式转化为DBObject格式
在mongodb中,可以使用com.mongodb.util.JSON类,将JSON格式的字符串转变为DBObject对象。MongoDB for JAVA驱动中提供了用于向数据库中存储普通对象的接口DBObject,当一个文档从MongoDB中取出时,它会自动把文档转换成DBObject接口类型,要将它实例化为需要的对象。比如:
{
'name' : 'mkyong',
'age' : 30
}
'name' : 'mkyong',
'age' : 30
}
这样的JSON格式字符串,转换方法为:
DBObject dbObject =(DBObject) JSON.parse("{'name':'mkyong', 'age':30}");
完整的代码如下:
packagecom.mkyong.core;
importjava.net.UnknownHostException;
importcom.mongodb.DB;
importcom.mongodb.DBCollection;
importcom.mongodb.DBCursor;
importcom.mongodb.DBObject;
importcom.mongodb.Mongo;
importcom.mongodb.MongoException;
importcom.mongodb.util.JSON;
/**
* Java MongoDB : Convert JSON data to DBObject
*
*/
publicclass App {
publicstaticvoid main(String[] args){
try{
Mongo mongo =new Mongo("localhost", 27017);
DB db = mongo.getDB("yourdb");
DBCollection collection = db.getCollection("dummyColl");
DBObject dbObject =(DBObject) JSON
.parse("{'name':'mkyong', 'age':30}");
collection.insert(dbObject);
DBCursor cursorDoc = collection.find();
while(cursorDoc.hasNext()){
System.out.println(cursorDoc.next());
}
System.out.println("Done");
}catch(UnknownHostException e){
e.printStackTrace();
}catch(MongoException e){
e.printStackTrace();
}
}
}
importjava.net.UnknownHostException;
importcom.mongodb.DB;
importcom.mongodb.DBCollection;
importcom.mongodb.DBCursor;
importcom.mongodb.DBObject;
importcom.mongodb.Mongo;
importcom.mongodb.MongoException;
importcom.mongodb.util.JSON;
/**
* Java MongoDB : Convert JSON data to DBObject
*
*/
publicclass App {
publicstaticvoid main(String[] args){
try{
Mongo mongo =new Mongo("localhost", 27017);
DB db = mongo.getDB("yourdb");
DBCollection collection = db.getCollection("dummyColl");
DBObject dbObject =(DBObject) JSON
.parse("{'name':'mkyong', 'age':30}");
collection.insert(dbObject);
DBCursor cursorDoc = collection.find();
while(cursorDoc.hasNext()){
System.out.println(cursorDoc.next());
}
System.out.println("Done");
}catch(UnknownHostException e){
e.printStackTrace();
}catch(MongoException e){
e.printStackTrace();
}
}
}
则输出为:
{"_id" : {"$oid" : "4dc9ebb5237f275c2fe4959f"} , "name" : "mkyong" , "age" : 30}
Done
可以看到,将JSON格式的数据类型直接转换为mongodb中的文档类型并输出。
小结:
本文学习了如何使用Mongodb for JAVA驱动,对mongodb进行日常的数据库操作,比如增加,删除和修改,下一篇教程中,将指导学习Spring对mongodb的操作。