本文整理匯總了Java中com.mongodb.client.MongoCursor.close方法的典型用法代碼示例。如果您正苦於以下問題:Java MongoCursor.close方法的具體用法?Java MongoCursor.close怎麽用?Java MongoCursor.close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.mongodb.client.MongoCursor
的用法示例。
在下文中一共展示了MongoCursor.close方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: findOne
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
/**
* 查詢一個
*
* @param collectionName 集合名
* @param query 查詢條件
* @param fields 返回字段或者排除字段
* @param sort
* @return
*/
public Map<String, Object> findOne(
String collectionName,
MongodbQuery query, MongodbFields fields, MongodbSort sort) {
MongoCollection<Document> collection = sMongoDatabase.getCollection(collectionName);
FindIterable<Document> findIterable = collection.find(query.getQuery());
if (fields == null) {
findIterable.projection(new MongodbFields().getDbObject());
} else {
findIterable.projection(fields.getDbObject());
}
if (sort != null) {
findIterable.sort(sort.getDbObject());
}
findIterable.limit(1);
MongoCursor<Document> cursor = findIterable.iterator();
try {
if (cursor.hasNext()) {
return cursor.next();
}
} finally {
cursor.close();
}
return null;
}
示例2: findAndConsumer
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
/**
* 查詢並逐條處理
*
* @param collectionName 集合名
* @param query 查詢條件
* @param fields 返回字段或者排除字段
* @param sort 排序方式
* @param consumer 記錄處理
* @return
*/
public void findAndConsumer(
String collectionName,
MongodbQuery query, MongodbFields fields,
MongodbSort sort, Consumer<Map<String, Object>> consumer) {
MongoCollection<Document> collection = sMongoDatabase.getCollection(collectionName);
FindIterable<Document> findIterable = collection.find(query == null ? new Document() : query.getQuery());
if (fields == null) {
findIterable.projection(new MongodbFields().getDbObject());
} else {
findIterable.projection(fields.getDbObject());
}
if (sort != null) {
findIterable.sort(sort.getDbObject());
}
MongoCursor<Document> cursor = findIterable.iterator();
try {
while (cursor.hasNext()) {
Map<String, Object> document = cursor.next();
consumer.accept(document);
}
} finally {
cursor.close();
}
}
示例3: findAll
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
/**
* 查詢
*
* @param clazz 類
* @param collectionName 集合名
* @param sort 排序
* @param <T>
* @return
*/
public <T> List<T> findAll(Class<T> clazz, String collectionName, MongodbSort sort) {
List<T> resultMapList = new ArrayList<T>();
MongoCollection<Document> collection = sMongoDatabase.getCollection(collectionName);
FindIterable<Document> findIterable = collection.find();
if(sort != null) {
findIterable.sort(sort.getDbObject());
}
MongoCursor<Document> cursor = findIterable.iterator();
try {
while (cursor.hasNext()) {
Document document = cursor.next();
T parseObject = JSON.parseObject(JSON.toJSONString(document), clazz);
resultMapList.add(parseObject);
}
} finally {
cursor.close();
}
return resultMapList;
}
示例4: executeQuery
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
@Override
long executeQuery(int threadId, long threadRunCount, long globalRunCount, long selectorId, long randomId){
final MongoCursor<Document> cursor = mongoCollection.find(eq(queriedField, selectorId)).iterator();
//final MongoCursor<Document> cursor = mongoCollection.find(in(queriedField, selectorId, selectorId+1, selectorId+2, selectorId+3, selectorId+4)).iterator();
long result = 0;
try {
while (cursor.hasNext()) {
final Document doc = cursor.next();
LOG.debug("Document {}", doc.toJson());
result++;
}
} finally {
cursor.close();
}
return result;
}
示例5: getAll
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
/**
* @return
*/
public List<Park> getAll() {
System.out.println("[DEBUG] MongoDBConnection.getAll()");
ArrayList<Park> allParksList = new ArrayList<Park>();
if (mongoDB != null) {
try {
MongoCollection parks = mongoDB.getCollection(COLLECTION);
MongoCursor<Document> cursor = parks.find().iterator();
try {
while (cursor.hasNext()) {
allParksList.add(ParkReadConverter.convert(cursor.next()));
}
} finally {
cursor.close();
}
} catch (Exception e) {
System.out.println("[ERROR] Error connecting to MongoDB. " + e.getMessage());
}
} else {
System.out.println("[ERROR] mongoDB could not be initiallized. No operation with DB will be performed");
}
return allParksList;
}
示例6: getByQuery
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
/**
* @param query
* @return
*/
public List<Park> getByQuery(BasicDBObject query) {
System.out.println("[DEBUG] MongoDBConnection.getByQuery()");
List<Park> parks = new ArrayList<Park>();
if (mongoDB != null) {
try {
MongoCursor<Document> cursor = mongoDB.getCollection(COLLECTION).find(query).iterator();
int i = 0;
try {
while (cursor.hasNext()) {
parks.add(ParkReadConverter.convert(cursor.next()));
}
} finally {
cursor.close();
}
} catch (Exception e) {
System.out.println("[ERROR] Error connecting to MongoDB. " + e.getMessage());
}
} else {
System.out.println("[ERROR] mongoDB could not be initiallized. No operation with DB will be performed");
}
return parks;
}
示例7: groupByWhereManyRange
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
@Override
public Object groupByWhereManyRange() throws Exception {
MongoCollection collection = database.getCollection("myCollection");
MongoCursor<Document> cursor = collection.aggregate(
Arrays.asList(
Document.parse("{$match: {" +
"$and: [" +
"{\"vlc\": {$gt: " + DemoData.RANGE_LEFT + ", $lt: " + DemoData.RANGE_RIGHT + "}}, {\"vch\": {$gt: " + DemoData.RANGE_LEFT + ", $lt: " + DemoData.RANGE_RIGHT + "}}" +
"]}}"),
Document.parse("{$group: {_id: {prg: \"$prg\", prr: \"$prr\"}, count: {$sum: 1}}}")
)
).iterator();
Map<Integer, Map<Integer, Integer>> g = new HashMap<>();
groupBy(g, cursor);
cursor.close();
return g;
}
示例8: list
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
@Override
public List<NoteInfo> list(AuthenticationInfo subject) throws IOException {
syncId();
List<NoteInfo> infos = new LinkedList<>();
MongoCursor<Document> cursor = coll.find().iterator();
while (cursor.hasNext()) {
Document doc = cursor.next();
Note note = documentToNote(doc);
NoteInfo info = new NoteInfo(note);
infos.add(info);
}
cursor.close();
return infos;
}
示例9: buildCorpus
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
private void buildCorpus() {
corpus = new Corpus();
MongoCursor<Document> cursor = companies.find()
.noCursorTimeout(true).iterator();
while (cursor.hasNext()) {
Document company = cursor.next();
ArrayList list = (ArrayList) company.get("aliases");
for (int i = 0; i < list.size(); i++) {
CompanyDocument document = new CompanyDocument.Builder(list.get(i).toString())
.id(company.getObjectId("_id").toString() + "_" + i)
.country(company.getString("country"))
.build();
corpus.addDocument(document);
}
}
cursor.close();
}
示例10: getAllMoviesAsBSON
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
/**
* Get all movies in the collection as BSON documents, be careful using this when performance
* is needed
* @return The List of BSON documents
*/
public List<Document> getAllMoviesAsBSON() {
MongoCollection<Document> collection = database.getCollection(MOVIES_COLLECTION);
FindIterable<Document> find = collection.find();
List<Document> jsonArray = new ArrayList<>();
MongoCursor<Document> cursor = collection.find().iterator();
try {
while (cursor.hasNext()) {
jsonArray.add(cursor.next());
}
} finally {
cursor.close();
}
return jsonArray;
}
示例11: find
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
/**
* 查詢
*
* @param collectionName 集合名
* @param query 查詢條件
* @param fields 返回字段或者排除字段
* @param sort 排序方式
* @param pageInfo 分頁
* @return
*/
public List<Map<String, Object>> find(
String collectionName,
MongodbQuery query, MongodbFields fields,
MongodbSort sort, MongodbPageInfo pageInfo) {
List<Map<String, Object>> resultMapList = new ArrayList<Map<String, Object>>();
MongoCollection<Document> collection = sMongoDatabase.getCollection(collectionName);
FindIterable<Document> findIterable = collection.find(query == null ? new Document() : query.getQuery());
if (fields == null) {
findIterable.projection(new MongodbFields().getDbObject());
} else {
findIterable.projection(fields.getDbObject());
}
if (sort != null) {
findIterable.sort(sort.getDbObject());
}
if (pageInfo != null) {
int startPos = pageInfo.getPageIndex() * pageInfo.getPageSize();
int rows = pageInfo.getPageSize();
if (startPos > 0) {
findIterable.skip(startPos - 1);
}
findIterable.limit(rows);
}
MongoCursor<Document> cursor = findIterable.iterator();
try {
while (cursor.hasNext()) {
Map<String, Object> document = cursor.next();
resultMapList.add(document);
}
} finally {
cursor.close();
}
return resultMapList;
}
示例12: main
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception{
MongoDBDriver mongoDBDriver = new MongoDBDriver();
try{
MongoDBConfig mongoDBConfig = new MongoDBConfig();
//mongoDBConfig.setAddresses("61.171.123.234:27017");
mongoDBConfig.setAddresses("61.171.123.234:27017");
List<MongoDBCredential> credentials = new ArrayList<MongoDBCredential>();
MongoDBCredential credential = new MongoDBCredential();
credential.setDatabaseName("whatsmars-common");
credential.setUsername("whatsmars");
//credential.setPassword("haodai.com");
credential.setPassword("passwordiscommon");
credentials.add(credential);
mongoDBConfig.setCredentials(credentials);
mongoDBDriver.setConfiguration(mongoDBConfig);
mongoDBDriver.init();
MongoDBClient client = new MongoDBClient();
client.setDatabaseName("whatsmars-common");
client.setMongoDBDriver(mongoDBDriver);
ListCollectionsIterable<Document> documents = client.getDatabase().listCollections();
MongoCursor<Document> it = documents.iterator();
while (it.hasNext()) {
Document item = it.next();
System.out.println(item.toJson());
}
it.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
mongoDBDriver.close();
}
}
示例13: _cursor
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
private <T extends Bean> Cursor<T> _cursor(final Class<T> t, final MongoCursor<Document> cur) {
return new Cursor<T>() {
@Override
public boolean hasNext() {
return cur.hasNext();
}
@Override
public T next() {
try {
T b = t.newInstance();
b.load(cur.next(), null);
return b;
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
@Override
public void close() {
cur.close();
}
};
}
示例14: findMany
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
@Override
public List<Document> findMany(String docName, Document docFilter) {
List<Document> docs = new ArrayList<Document>();
MongoCursor<Document> cursor = baseFind(docName, docFilter);
while (cursor.hasNext()) {
docs.add(cursor.next());
}
cursor.close();
return docs;
}
示例15: findOne
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
@Override
public Document findOne(String docName, Document docFilter) {
MongoCursor<Document> cursor = baseFind(docName, docFilter);
Document doc = null;
if (cursor.hasNext()) {
doc = cursor.next();
}
cursor.close();
return doc;
}