本文整理汇总了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());
}
}
}