本文整理汇总了Java中com.mongodb.client.MongoCursor.hasNext方法的典型用法代码示例。如果您正苦于以下问题:Java MongoCursor.hasNext方法的具体用法?Java MongoCursor.hasNext怎么用?Java MongoCursor.hasNext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mongodb.client.MongoCursor
的用法示例。
在下文中一共展示了MongoCursor.hasNext方法的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: findAll
import com.mongodb.client.MongoCursor; //导入方法依赖的package包/类
@Override
public List<KeyValueConfigEntity> findAll(@Nonnull KeyValueConfigName configName) throws Exception {
Objects.requireNonNull(configName);
String collectionName = configName.getQualifiedName();
MongoCollection<RawBsonDocument> collection =
connector.getDatabase().getCollection(collectionName, RawBsonDocument.class);
MongoCursor<RawBsonDocument> it = collection.find().iterator();
if (!it.hasNext()) {
return Collections.emptyList();
}
RawBsonDocument document = it.next();
ByteArrayInputStream bin = new ByteArrayInputStream(document.getByteBuffer().array());
ObjectMapper objectMapper = MongoConfigObjectMapper.getInstance();
ObjectReader objectReader = objectMapper.readerFor(MongoConfigEntity.class);
List<KeyValueConfigEntity> result = ((MongoConfigEntity) objectReader.readValue(bin)).getConfig();
// set groupName on returned config key-value pairs
return result.stream().map(input -> input.setConfigName(configName)).collect(Collectors.toList());
}
示例5: queryTables
import com.mongodb.client.MongoCursor; //导入方法依赖的package包/类
/**
* @decription 查询数据库表名
* @author yi.zhang
* @time 2017年6月30日 下午2:16:02
* @param table 表名
* @return
*/
public List<String> queryTables(){
try {
if(session==null){
init(servers, database, schema, username, password);
}
MongoIterable<String> collection = session.listCollectionNames();
if (collection == null) {
return null;
}
List<String> tables = new ArrayList<String>();
MongoCursor<String> cursor = collection.iterator();
while(cursor.hasNext()){
String table = cursor.next();
tables.add(table);
}
return tables;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
示例6: getDocuments
import com.mongodb.client.MongoCursor; //导入方法依赖的package包/类
public List<Document> getDocuments(String keyName, String keyValue,
RESOURCE_TYPE resType, String sortKey, boolean asc, int limit) {
ArrayList<Document> docList = new ArrayList<Document>();
BasicDBObject query = new BasicDBObject(keyName, keyValue).append(
RESTYPE_KEY, resType.Value());
BasicDBObject sort = new BasicDBObject(sortKey, asc ? 1 : -1);
MongoCollection<Document> collection = context.getDatabaseManager()
.getCollection(collectionName);
MongoCursor<Document> cursor = collection.find(query).sort(sort)
.limit(limit).iterator();
while (cursor.hasNext()) {
docList.add(cursor.next());
}
return docList;
}
示例7: readAll
import com.mongodb.client.MongoCursor; //导入方法依赖的package包/类
@Override
public <P extends ParaObject> Map<String, P> readAll(String appid, List<String> keys, boolean getAllColumns) {
if (keys == null || keys.isEmpty() || StringUtils.isBlank(appid)) {
return new LinkedHashMap<String, P>();
}
Map<String, P> results = new LinkedHashMap<String, P>(keys.size(), 0.75f, true);
BasicDBObject inQuery = new BasicDBObject();
inQuery.put(ID, new BasicDBObject("$in", keys));
MongoCursor<Document> cursor = getTable(appid).find(inQuery).iterator();
while (cursor.hasNext()) {
Document d = cursor.next();
P obj = fromRow(d);
if (d != null) {
results.put(d.getString(ID), obj);
}
}
logger.debug("DAO.readAll() {}", results.size());
return results;
}
示例8: 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;
}
示例9: paginate
import com.mongodb.client.MongoCursor; //导入方法依赖的package包/类
/**
* 分页查询
*
* @param collection
* @param pageNumber
* @param pageSize
* @param filter
* @param like
* @param sort
* @return
*/
public static Page<Record> paginate(String collection, int pageNumber, int pageSize, BasicDBObject filter, BasicDBObject like, BasicDBObject sort) {
BasicDBObject conditons = new BasicDBObject();
buildFilter(filter, conditons);
buildLike(like, conditons);
MongoCursor<Document> cursor = getCollection(collection).find(conditons)
.skip((pageNumber - 1) * pageSize).limit(pageSize).sort(sort(sort)).iterator();
List<Record> records = new ArrayList<>();
Long totalRow = getCollection(collection).count(conditons);
while (cursor.hasNext()) {
records.add(toRecord(cursor.next()));
}
if (totalRow <= 0) {
return new Page<>(new ArrayList<Record>(0), pageNumber, pageSize, 0, 0);
}
Long totalPage = totalRow / pageSize;
if (totalRow % pageSize != 0) {
totalPage++;
}
Page<Record> page = new Page<>(records, pageNumber, pageSize, totalPage.intValue(), totalRow.intValue());
return page;
}
示例10: queryDataTest
import com.mongodb.client.MongoCursor; //导入方法依赖的package包/类
@Test
public void queryDataTest() {
BasicDBObject fields = new BasicDBObject();
fields.put("likes", new BasicDBObject("$gt", 16));
FindIterable<Document> iterable = mongoPool.getDB()
.getCollection("test").find(fields);
MongoCursor<Document> cursor = iterable.iterator();
System.out.println(cursor);
while (cursor.hasNext()) {
Document document = cursor.next();
System.out.println(document);
if (document.get("append") != null) {
System.out.println(document.get("append") instanceof Document);
}
}
}
示例11: updateStationWidthCluster
import com.mongodb.client.MongoCursor; //导入方法依赖的package包/类
private static void updateStationWidthCluster(){
//get city codes
HashMap<String, String> clusterIds = new HashMap<String, String>();
MongoClient client = new MongoClient("127.0.0.1");
MongoDatabase db = client.getDatabase("airdb");
MongoCollection clusters = db.getCollection("cluster");
MongoCursor cursor = clusters.find().iterator();
Document d;
while(cursor.hasNext()){
d = (Document)cursor.next();
clusterIds.put(d.getString("code"), d.getString("clusterid"));
}
MongoCollection month = db.getCollection("pm_stations");
cursor = month.find().iterator();
while(cursor.hasNext()){
d = (Document)cursor.next();
String code = d.getString("code");
String id = clusterIds.get(code);
month.updateOne(new Document("code", code),
new Document("$set", new Document("clusterid", id)));
}
}
示例12: getAllStationFromDB
import com.mongodb.client.MongoCursor; //导入方法依赖的package包/类
/**
* save all stations to a file allStationCodeCurrentDB
* @return
*/
private static ArrayList<String> getAllStationFromDB(){
MongoClient client = new MongoClient("127.0.0.1");
MongoDatabase db = client.getDatabase("pm");
MongoCollection pm = db.getCollection("pm25inoriginal");
MongoCursor cursor = pm.distinct("station_code", String.class).iterator();
ArrayList<String> result = new ArrayList<String>();
String temp = null;
while(cursor.hasNext()){
temp = cursor.next().toString();
result.add(temp);
try {
FileUtils.writeStringToFile(new File(PATH+"allStationCodeCurrentDB"), temp+"\n", true);
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
示例13: connectsToTheCluster
import com.mongodb.client.MongoCursor; //导入方法依赖的package包/类
@Test
@IgnoreIf(condition = NoRunningMongoDbForTests.class)
public void connectsToTheCluster() throws Exception {
MongoClient mongoClient = new MongoClient(new NoRunningMongoDbForTests().getHost(), 27017);
MongoIterable<String> strings = mongoClient.listDatabaseNames();
MongoCursor<String> iterator = strings.iterator();
while (iterator.hasNext()) {
LOG.info("Database: {}", iterator.next());
}
MongoDatabase db = mongoClient.getDatabase(strings.iterator().next());
String name = db.getName();
LOG.info("Got: {}", name);
mongoClient.close();
}
示例14: generateESPMResult
import com.mongodb.client.MongoCursor; //导入方法依赖的package包/类
private void generateESPMResult() throws WikitException {
this.resultColl.drop();
MongoCollection<Document> esaColl = MongoClient.getOriginCollection(conf,
"expt.odp.esa");
MongoCursor<Document> esaCursor = esaColl.find().iterator();
ProgressCounter progress = new ProgressCounter(esaColl.count());
while (esaCursor.hasNext()) {
Document item = esaCursor.next();
String path = (String)item.get("path");
List<Integer> ids = (List<Integer>)item.get("ids");
List<Double> esaScores = (List<Double>)item.get("scores");
ConceptVector cv = new TroveConceptVector(ids.size());
for (int i = 0; i < ids.size(); i++) {
cv.add(ids.get(i), esaScores.get(i));
}
matchComapre(this.resultColl, path, cv, this.esaCptLimit);
progress.increment();
}
progress.done();
}
示例15: preProcess
import com.mongodb.client.MongoCursor; //导入方法依赖的package包/类
/**
* parse date and other digital
*/
private static void preProcess() {
try {
MongoCollection weatherRaw = db.getCollection("weatherRaw");
MongoCollection weather = db.getCollection("weather");
MongoCursor cursor = weatherRaw.find().iterator();
while (cursor.hasNext()) {
Document insert = new Document();
Document info = (Document) cursor.next();
String year = info.getString("Year");
String month = info.getString("Mon");
String day = info.getString("Day");
insert.append("station", Integer.parseInt(info.getString("Station_Id_C")));
insert.append("time", df.parse(year + "-" + month + "-" + day + " +0000"));
for(String h : header){
if(h.equals("Year") || h.equals("Mon") || h.equals("Day"))
continue;
insert.append(h, Double.parseDouble(info.getString(h)));
}
weather.insertOne(insert);
}
}catch(Exception e){
e.printStackTrace();
}
}