當前位置: 首頁>>代碼示例>>Java>>正文


Java MongoCursor類代碼示例

本文整理匯總了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;
}
 
開發者ID:wxz1211,項目名稱:dooo,代碼行數:34,代碼來源:MongodbDataAccess.java

示例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;
}
 
開發者ID:ibmruntimes,項目名稱:acmeair-modular,代碼行數:25,代碼來源:BookingServiceImpl.java

示例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();
    }
}
 
開發者ID:wxz1211,項目名稱:dooo,代碼行數:35,代碼來源:MongodbDataAccess.java

示例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;
}
 
開發者ID:wxz1211,項目名稱:dooo,代碼行數:30,代碼來源:MongodbDataAccess.java

示例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());
}
 
開發者ID:scalecube,項目名稱:config,代碼行數:23,代碼來源:MongoConfigRepository.java

示例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;
}
 
開發者ID:dev-share,項目名稱:database-transform-tool,代碼行數:30,代碼來源:MongoDBFactory.java

示例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;
}
 
開發者ID:ysrc,項目名稱:Liudao,代碼行數:28,代碼來源:MongoDao.java

示例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;
}
 
開發者ID:idealo,項目名稱:mongodb-performance-test,代碼行數:18,代碼來源:IterateOperation.java

示例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());
	}
}
 
開發者ID:PinaeOS,項目名稱:pumbaa,代碼行數:24,代碼來源:MongodbService.java

示例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);
	}
}
 
開發者ID:giiwa,項目名稱:giiwa,代碼行數:20,代碼來源:MongoHelper.java

示例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;
}
 
開發者ID:thankjava,項目名稱:java-toolkit,代碼行數:17,代碼來源:MongoDriverManager.java

示例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);
		}
	}
}
 
開發者ID:lolog,項目名稱:mogodb-dao,代碼行數:19,代碼來源:MongoAndSpringTest.java

示例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();
}
 
開發者ID:lucko,項目名稱:LuckPerms,代碼行數:21,代碼來源:MongoDao.java

示例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;
}
 
開發者ID:lucko,項目名稱:LuckPerms,代碼行數:22,代碼來源:MongoDao.java

示例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();
}
 
開發者ID:lucko,項目名稱:LuckPerms,代碼行數:21,代碼來源:MongoDao.java


注:本文中的com.mongodb.client.MongoCursor類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。