使用Java驱动程序从MongoDB获取结果的更好方法
我是MongoDB的新手。我正在使用MongoDB和Java-driver-3.2.2。早些时候,我使用下面的代码从数据库中获取结果使用Java驱动程序从MongoDB获取结果的更好方法,mongodb,mongodb-java,Mongodb,Mongodb Java,我是MongoDB的新手。我正在使用MongoDB和Java-driver-3.2.2。早些时候,我使用下面的代码从数据库中获取结果 MongoClient mongoClient = new MongoClient("localhost", 27017); @SuppressWarnings("deprecation") DB deviceBaseDB = mongoClient.getDB("test"); DBCollection deviceBaseCollection = device
MongoClient mongoClient = new MongoClient("localhost", 27017);
@SuppressWarnings("deprecation")
DB deviceBaseDB = mongoClient.getDB("test");
DBCollection deviceBaseCollection = deviceBaseDB.getCollection("myFirstCollection");
List<String> list = new ArrayList<String>();
DBCursor cursor = deviceBaseCollection.find(queryObject, projection)
.sort(sortingCriteriea).limit(10);
while (cursor.hasNext()) {
list.add(cursor.next().toString());
}
But in the above code mongoClient.getDB() call is depricated. So I am trying to use New API() from MongoDB like below.
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("myFirstCollection");
// First approach
List<Document> listDocs = collection.find().projection(projection)
.sort(new BasicDBObject("likes", -1)).into(new ArrayList<Document>());
List<String> strList1 = new ArrayList<String>();
for(Document doc : listDocs) {
strList1.add(doc.toJson());
}
System.out.println("List of json files strList1:: " + strList1);
// Second Approach
FindIterable<Document> iterableDocsWithFilter_SortWithProject = collection.find(
new BasicDBObject("title", "New MongoDB Learning")).projection(projection)
.sort(new BasicDBObject("likes", -1));
Iterator<Document> iterator = iterableDocsWithFilter_SortWithProject.iterator();
List<String> strList2 = new ArrayList<String>();
while (iterator.hasNext()) {
strList2.add(iterator.next().toJson());
}
System.out.println("List of json files strList2:: " + strList2);
// Third Approach
MongoCursor<Document> mongoCursor = (MongoCursor<Document>) collection.find(
new BasicDBObject("title", "New MongoDB Learning")).projection(projection)
.sort(new BasicDBObject("likes", -1));
List<String> strList3 = new ArrayList<String>();
while(mongoCursor.hasNext()) {
strList3.add(mongoCursor.next().toJson());
}
System.out.println("List of json files strList3:: " + strList3);
MongoClient-MongoClient=newmongoclient(“localhost”,27017);
@抑制警告(“弃用”)
DB deviceBaseDB=mongoClient.getDB(“测试”);
DBCollection deviceBaseCollection=deviceBaseDB.getCollection(“myFirstCollection”);
列表=新的ArrayList();
DBCursor cursor=deviceBaseCollection.find(查询对象,投影)
.排序(排序标准)。限制(10);
while(cursor.hasNext()){
添加(cursor.next().toString());
}
但是在上面的代码中,mongoClient.getDB()调用是去擦洗的。因此,我尝试使用来自MongoDB的新API(),如下所示。
MongoDatabase=mongoClient.getDatabase(“测试”);
MongoCollection collection=database.getCollection(“myFirstCollection”);
//第一种方法
List listDocs=collection.find().projection(projection)
.sort(新的BasicDBObject(“likes”,-1)).to(新的ArrayList());
List strList1=newarraylist();
对于(文档文档:listDocs){
添加(doc.toJson());
}
System.out.println(“json文件列表strList1::”+strList1);
//第二种方法
FindItemerable iterableDocsWithFilter\u SortWithProject=collection.find(
新的基本主题(“标题”,“新MongoDB学习”)。投影(投影)
.sort(新的基本对象(“likes”、-1));
Iterator Iterator=iterableDocsWithFilter_SortWithProject.Iterator();
List strList2=newarraylist();
while(iterator.hasNext()){
添加(iterator.next().toJson());
}
System.out.println(“json文件列表strList2::”+strList2);
//第三种方法
MongoCursor MongoCursor=(MongoCursor)collection.find(
新的基本主题(“标题”,“新MongoDB学习”)。投影(投影)
.sort(新的基本对象(“likes”、-1));
List strList3=新的ArrayList();
while(mongoCursor.hasNext()){
添加(mongoCursor.next().toJson());
}
System.out.println(“json文件列表strList3::”+strList3);
我的问题如下:
1) 新API(document.toJson())是否会比旧的去擦洗API(使用toString(DBCursor))提高性能?
2) 我找到了3种使用新API从mongoDB获取结果的方法,在这3种方法中,哪一种更好?1)\uToJSON和toString的功能略有不同。toJson将返回文档的JSON表示,而toString将提供默认字符串输出
如果您已经实现了自定义toString,那么性能取决于您的toString实现
如果您使用的是默认toString,那么toJson应该比toString花费更多的时间。您可以使用System.currentTimeMillis()来验证这一点
2) _uu所有3种方式对于DB上的CRUD同样合法。现在,如果您考虑选择哪一个,这取决于您的应用程序和您需要做什么
如果您只想使用几个gt、lt等过滤器检查快速查询,那么您应该使用第一种方法;您可以列出整个查询结果并显示它。简单易用
另一方面,如果您想对检索到的结果执行一些任务,那么我建议使用第二种或第三种方法。请记住FindTable和MongoCursor之间的使用略有不同
MongoCursor是一个简单的迭代器,仅此而已;而FindTable允许您构建查询。下面的引文可以让我们对FindTable有所了解
实际上,FindTable类似于old的DBCursor,类似于
DBCursor是Iterable接口的一个实现。所以你可以
对于循环查询结果,也要以类似的方式处理它。关键区别在于
FindTable是比DBCursor更高的抽象,它是
可链接界面,允许用户以流畅的方式建立查询
方法是更改过滤器和选项。在最高级别
FindItemable是MongoInterable接口的一个实现
表示某些MongoDB操作(无论是查询还是查询)的结果
指挥部。
因此,如果您只想查看一次查询结果,并可能对每个结果执行一个简单的任务,您可以求助于MongoCursor
我个人坚持使用FindItemable,因为它提供了所有其他方法所能提供的功能以及更多功能