3
下面的代碼找到的第一個文件集合中:查找集合中的所有對象的Java MongoDB的
package database;
import com.mongodb.BasicDBObject;
import com.mongodb.BulkWriteOperation;
import com.mongodb.BulkWriteResult;
import com.mongodb.Cursor;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ParallelScanOptions;
import com.mongodb.ServerAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Set;
import static java.util.concurrent.TimeUnit.SECONDS;
// based on http://mongodb.github.io/mongo-java-driver/2.13/getting-started/quick-tour/
public class Mongo {
public void getCon() {
// or
MongoClient mongoClient;
try {
mongoClient = new MongoClient("localhost", 27017);
DB db = mongoClient.getDB("mydb");
DBCollection coll = db.getCollection("testCollection");
BasicDBObject doc = new BasicDBObject("name", "MongoDB")
.append("type", "database")
.append("count", 1)
.append("info",
new BasicDBObject("x", 203).append("y", 102));
coll.insert(doc);
coll.findOne();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
似乎沒有成爲一個的findAll方法。如何查找集合testCollection
中的所有文檔?