本文整理汇总了Java中com.mongodb.client.MongoCursor类的典型用法代码示例。如果您正苦于以下问题:Java MongoCursor类的具体用法?Java MongoCursor怎么用?Java MongoCursor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MongoCursor类属于com.mongodb.client包,在下文中一共展示了MongoCursor类的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: getBookingsByUser
import com.mongodb.client.MongoCursor; //导入依赖的package包/类
@Override
public List<String> getBookingsByUser(String user) {
List<String> bookings = new ArrayList<String>();
if(logger.isLoggable(Level.FINE)){
logger.fine("getBookingsByUser : " + user);
}
try (MongoCursor<Document> cursor = booking.find(eq("customerId", user)).iterator()){
while (cursor.hasNext()){
Document tempBookings = cursor.next();
Date dateOfBooking = (Date)tempBookings.get("dateOfBooking");
tempBookings.remove("dateOfBooking");
tempBookings.append("dateOfBooking", dateOfBooking.toString());
if(logger.isLoggable(Level.FINE)){
logger.fine("getBookingsByUser cursor data : " + tempBookings.toJson());
}
bookings.add(tempBookings.toJson());
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return bookings;
}
示例3: 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();
}
}
示例4: 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;
}
示例5: 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());
}
示例6: 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;
}
示例7: 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;
}
示例8: 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;
}
示例9: find
import com.mongodb.client.MongoCursor; //导入依赖的package包/类
private Object find(String filter) throws ServiceException {
try {
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
FindIterable<Document> cursor = null;
if (filter != null) {
BasicDBObject filterObj = BasicDBObject.parse(filter);
cursor = this.collection.find(filterObj);
} else {
cursor = this.collection.find();
}
if (cursor != null) {
MongoCursor<Document> iterator = cursor.iterator();
while (iterator.hasNext()) {
result.add(iterator.next());
}
}
return result;
} catch (Exception e) {
throw new ServiceException("Search Exception: " + e.getMessage());
}
}
示例10: _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);
}
}
示例11: 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;
}
示例12: 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);
}
}
}
示例13: getUsersWithPermission
import com.mongodb.client.MongoCursor; //导入依赖的package包/类
@Override
public List<HeldPermission<UUID>> getUsersWithPermission(String permission) {
ImmutableList.Builder<HeldPermission<UUID>> held = ImmutableList.builder();
MongoCollection<Document> c = this.database.getCollection(this.prefix + "users");
try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) {
Document d = cursor.next();
UUID holder = d.get("_id", UUID.class);
Set<NodeModel> nodes = new HashSet<>(nodesFromDoc(d));
for (NodeModel e : nodes) {
if (!e.getPermission().equalsIgnoreCase(permission)) {
continue;
}
held.add(NodeHeldPermission.of(holder, e));
}
}
}
return held.build();
}
示例14: createAndLoadGroup
import com.mongodb.client.MongoCursor; //导入依赖的package包/类
@Override
public Group createAndLoadGroup(String name) {
Group group = this.plugin.getGroupManager().getOrMake(name);
group.getIoLock().lock();
try {
MongoCollection<Document> c = this.database.getCollection(this.prefix + "groups");
try (MongoCursor<Document> cursor = c.find(new Document("_id", group.getName())).iterator()) {
if (cursor.hasNext()) {
Document d = cursor.next();
Set<Node> nodes = nodesFromDoc(d).stream().map(NodeModel::toNode).collect(Collectors.toSet());
group.setEnduringNodes(nodes);
} else {
c.insertOne(groupToDoc(group));
}
}
} finally {
group.getIoLock().unlock();
}
group.getRefreshBuffer().requestDirectly();
return group;
}
示例15: getGroupsWithPermission
import com.mongodb.client.MongoCursor; //导入依赖的package包/类
@Override
public List<HeldPermission<String>> getGroupsWithPermission(String permission) {
ImmutableList.Builder<HeldPermission<String>> held = ImmutableList.builder();
MongoCollection<Document> c = this.database.getCollection(this.prefix + "groups");
try (MongoCursor<Document> cursor = c.find().iterator()) {
while (cursor.hasNext()) {
Document d = cursor.next();
String holder = d.getString("_id");
Set<NodeModel> nodes = new HashSet<>(nodesFromDoc(d));
for (NodeModel e : nodes) {
if (!e.getPermission().equalsIgnoreCase(permission)) {
continue;
}
held.add(NodeHeldPermission.of(holder, e));
}
}
}
return held.build();
}