本文整理匯總了Java中com.mongodb.client.MongoCursor.next方法的典型用法代碼示例。如果您正苦於以下問題:Java MongoCursor.next方法的具體用法?Java MongoCursor.next怎麽用?Java MongoCursor.next使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.mongodb.client.MongoCursor
的用法示例。
在下文中一共展示了MongoCursor.next方法的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: sortMap
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
/**
* 根據統計字段計算統計結果(gte最小值)並排序
*
* @param collectionName 集合名
* @param match match條件
* @param field 統計字段
* @param minCount 最小值
* @return
*/
public LinkedHashMap<String, Integer> sortMap(String collectionName, Document match, String field, int minCount) {
AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
Arrays.asList(
match(match)
, group("$" + field, Accumulators.sum("_count", 1))
, match(new Document("_count", new Document("$gte", minCount)))
, sort(new Document("_count", -1))
)
);
LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
MongoCursor<Document> iterator = aggregate.iterator();
while (iterator.hasNext()) {
Document next = iterator.next();
map.put(next.getString("_id"), next.getInteger("_count"));
}
return map;
}
示例7: 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;
}
示例8: _backup
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
private void _backup(PrintStream out, String tablename) {
log.debug("backuping " + tablename);
MongoCollection<Document> d1 = getCollection(Helper.DEFAULT, tablename);
MongoCursor<Document> c1 = d1.find().iterator();
int rows = 0;
while (c1.hasNext()) {
rows++;
Document d2 = c1.next();
JSON jo = new JSON();
jo.put("_table", tablename);
for (String name : d2.keySet()) {
jo.put(name, d2.get(name));
}
out.println(jo.toString());
if (rows % 1000 == 0)
log.debug("backup " + tablename + ", rows=" + rows);
}
}
示例9: findByObjectId
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
@Override
public Document findByObjectId(String docName, String objectHexString) {
if (objectHexString == null) {
return null;
}
ObjectId objectId = new ObjectId(objectHexString);
Document doc = new Document();
doc.put(ObjectIdKey, objectId);
MongoCursor<Document> cursor = baseFind(docName, doc);
doc = null;
if (cursor.hasNext()) {
doc = cursor.next();
}
return doc;
}
示例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: 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;
}
示例12: testEmptyFilter
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
@Test
public void testEmptyFilter() {
adapter.deleteDatabase();
adapter.setupEmptyDatabase();
int nMoviesInTest = fillDBWithMovies();
Filter emptyFilter = new Filter();
//filter empty
int count = 0;
MongoCursor<Document> filteredListOfMovieDocuments =
adapter.getFilteredListOfMovieDocuments(emptyFilter);
while (filteredListOfMovieDocuments.hasNext()) {
Document next = filteredListOfMovieDocuments.next();
count++;
}
assertEquals(nMoviesInTest, count);
}
示例13: updateProvinceName
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
/**
* 將省份名稱添加到pmdata_month中
* 在insertProvinceName後調用
*/
private static void updateProvinceName(){
//get city codes
HashMap<String, String> provCode = new HashMap<String, String>();
MongoClient client = new MongoClient("127.0.0.1");
MongoDatabase db = client.getDatabase("pm");
MongoCollection locWithLL = db.getCollection("loc_ll_google");
MongoCursor cursor = locWithLL.find().iterator();
Document d;
while(cursor.hasNext()){
d = (Document)cursor.next();
provCode.put(d.getString("code"), d.getString("province"));
// System.out.println(d.getString("code") + ":" + d.getString("province"));
}
MongoCollection month = db.getCollection("pmdata_month");
cursor = month.find().iterator();
while(cursor.hasNext()){
d = (Document)cursor.next();
String code = d.getString("code");
String province = provCode.get(code);
// System.out.println(province);
month.updateOne(new Document("_id", d.get("_id")),
new Document("$set", new Document("province", province)));
}
}
示例14: removeDuplicate
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
/**
* removeDuplicate
* by add a unique index
*/
private static void removeDuplicate(){
MongoClient client = new MongoClient("127.0.0.1");
MongoDatabase db = client.getDatabase("airvis");
MongoCollection preprocess = db.getCollection("pm_preProcess");
MongoCollection process = db.getCollection("pmProcess");
IndexOptions option = new IndexOptions();
option.unique(true);
process.createIndex(new Document().append("time",1).append("code",1), option);
MongoCursor cur = preprocess.find().iterator();
while(cur.hasNext()){
Document obj = (Document)cur.next();
try {
process.insertOne(obj);
}catch(MongoWriteException e){
//duplicate error
System.out.println(obj.toString());
}
}
}
示例15: removeDuplicate
import com.mongodb.client.MongoCursor; //導入方法依賴的package包/類
/**
* removeDuplicate
* by add a unique index
*/
private static void removeDuplicate(){
MongoClient client = new MongoClient("127.0.0.1");
MongoDatabase db = client.getDatabase("pm");
MongoCollection preprocess = db.getCollection("pm_preProcess");
MongoCollection process = db.getCollection("pmProcess");
IndexOptions option = new IndexOptions();
option.unique(true);
process.createIndex(new Document().append("time",1).append("code",1), option);
MongoCursor cur = preprocess.find().iterator();
while(cur.hasNext()){
Document obj = (Document)cur.next();
try {
process.insertOne(obj);
}catch(MongoWriteException e){
//duplicate error
System.out.println(obj.toString());
}
}
}