MongoDB 的 Java 语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30424894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java syntax with MongoDB
提问by Hal50000
I'm going through the intro to MongoDB for java. There's some example code to retrieve all the documents in a collection. The code works, but I find it a bit...clunky for lack of a better word. I'm wondering if there's a specific reason that makes it necessary. The given example is:
我正在介绍 MongoDB for Java。有一些示例代码可以检索集合中的所有文档。该代码有效,但我觉得它有点……笨拙,因为缺少更好的词。我想知道是否有特定的原因使它有必要。给定的例子是:
FindIterable<Document> iterable = db.getCollection("restaurants").find();
iterable.forEach(new Block<Document>() {
@Override
public void apply(final Document document) {
System.out.println(document);
}
});
Is there some reason a Block
instance has to be created in every iteration of the forEach
in the above example? Why not something a little more straightforward like:
是否有某种原因Block
必须在上例中的每次迭代中创建一个forEach
实例?为什么不做一些更简单的事情,比如:
FindIterable<Document> iterable = db.getCollection("restaurants").find();
for (Document document : iterable) {
System.out.println(document);
}
采纳答案by jyemin
While you can certainly use the form that you suggested:
虽然您当然可以使用您建议的表格:
for (Document document : col.find()) {
// do something
}
it introduces a problem when the body of the for loop throws an exception: if this happens the cursor will not be closed. The proper idiom to guard against that is to use MongoCursor (which implements Closeable) explicitly:
当 for 循环的主体抛出异常时,它会引入一个问题:如果发生这种情况,游标将不会关闭。防止这种情况的正确习惯用法是显式使用 MongoCursor(实现 Closeable):
try (MongoCursor<Document> cursor = col.find().iterator()) {
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
}
The forEach method is just a bit of syntactic sugar to avoid the need for application code to worry about having to close the cursor manually like this.
forEach 方法只是一些语法糖,以避免应用程序代码担心必须像这样手动关闭游标。
If you don't want to create a new Block for each iteration, you can refactor your code pull out the anonymous inner class creation, e.g.:
如果你不想为每次迭代创建一个新的 Block,你可以重构你的代码拉出匿名内部类的创建,例如:
Block<Document> block = new Block<Document>() {
@Override
public void apply(final Document document) {
System.out.println(document);
}
};
col.find().forEach(block);
Of course that's even clunkier, so if you are able to use Java 8, you can replace the whole thing with a lambda:
当然,这更笨拙,因此如果您能够使用 Java 8,则可以用 lambda 替换整个内容:
col.find().forEach((Block<Document>) document -> {
System.out.println(document);
});
or in this case simply:
或者在这种情况下简单地:
col.find().forEach((Block<Document>) System.out::println);
The lambda metafactorywill ensure that no unnecessary objects are created.
该拉姆达metafactory将确保不产生不必要的对象。
回答by Weslor
I asked myself the same question and I found pretty easy the following code to handle that situation:
我问自己同样的问题,我发现以下代码很容易处理这种情况:
List<Document> restaurants = db.getCollection("restaurants").find().into(new ArrayList<Document>());
for (Document restaurant : restaurants) {
System.out.println(restaurant);
}