BasicDBObject query = new BasicDBObject(); BasicDBObject field = new BasicDBObject(); field.put("HomeTown", 1); DBCursor cursor = db.getCollection(collectionName).find(query,field); while (cursor.hasNext()) { BasicDBObject obj = (BasicDBObject) cursor.next(); result.add(obj.getString("HomeTown")); }
private Timestamp<?> getCurrentOplogTimestamp() { try (DBCursor cursor = oplogCollection.find().sort(new BasicDBObject(MongoDBRiver.INSERTION_ORDER_KEY, -1)).limit(1)) { return Timestamp.on(cursor.next()); } }
checkPermission(RestPermissions.METRICS_READHISTORY, metricName); BasicDBObject andQuery = new BasicDBObject(); obj.add(new BasicDBObject("name", metricName)); if (after != -1) { obj.add(new BasicDBObject("$gt", new BasicDBObject("$gt", new Date(after)))); try(DBCursor cursor = dbCollection.find(andQuery).sort(new BasicDBObject("timestamp", 1))) { final Map<String, Object> metricsData = Maps.newHashMap(); metricsData.put("name", metricName); metricsData.put("values", values); while (cursor.hasNext()) { final DBObject value = cursor.next(); metricsData.put("node", value.get("node"));
/** * Finds a list of files matching the given query. * * @param query the filter to apply * @param sort the fields to sort with * @return list of gridfs files * @throws com.mongodb.MongoException if the operation fails */ public List<GridFSDBFile> find(final DBObject query, final DBObject sort) { List<GridFSDBFile> files = new ArrayList<GridFSDBFile>(); DBCursor cursor = filesCollection.find(query); if (sort != null) { cursor.sort(sort); } try { while (cursor.hasNext()) { files.add(injectGridFSInstance(cursor.next())); } } finally { cursor.close(); } return Collections.unmodifiableList(files); }
cr = mongoAdminDB.command(new BasicDBObject("addShard", command)); logger.info(cr.toString()); cr = mongoAdminDB.command(new BasicDBObject("listShards", 1)); logger.info(cr.toString()); cr = mongoAdminDB.command(new BasicDBObject("enableSharding", this.shardDatabase)); logger.info(cr.toString()); DBCursor cursor = mongo.getDB("config").getCollection("shards").find(); while (cursor.hasNext()) { DBObject item = cursor.next(); logger.info(item.toString());
BasicDBObject searchQuery = new BasicDBObject(); DBCursor cursor = table.find(searchQuery); while (cursor.hasNext()) { System.out.println(cursor.next().keySet()); }
@Override public List<MongoEndpointNotification> findNotificationsByKeyHash(final byte[] keyHash) { LOG.debug("Find unicast notifications by endpoint key hash [{}] ", keyHash); DBObject dbObject = query(where(EP_ENDPOINT_KEY_HASH).is(keyHash)).getQueryObject(); DBCursor cursor = mongoTemplate.getDb().getCollection(getCollectionName()).find(dbObject); List<MongoEndpointNotification> endpointNotifications = new ArrayList<>(); while (cursor.hasNext()) { endpointNotifications.add(mongoTemplate.getConverter() .read(MongoEndpointNotification.class, cursor.next())); } return endpointNotifications; }
private Timestamp<?> getCurrentOplogTimestamp(MongoClient shardClient) { DBCollection oplogCollection = shardClient .getDB(MongoDBRiver.MONGODB_LOCAL_DATABASE) .getCollection(MongoDBRiver.OPLOG_COLLECTION); try (DBCursor cursor = oplogCollection.find().sort(new BasicDBObject("$natural", -1)).limit(1)) { return Timestamp.on(cursor.next()); } }
@Test public void testOutNamedCollection() { checkMinServerVersion(2.6); getDs().save(asList(new Book("The Banquet", "Dante", 2, "Italian", "Sophomore Slump"), new Book("Divine Comedy", "Dante", 1, "Not Very Funny", "I mean for a 'comedy'", "Ironic"), new Book("Eclogues", "Dante", 2, "Italian", ""), new Book("The Odyssey", "Homer", 10, "Classic", "Mythology", "Sequel"), new Book("Iliad", "Homer", 10, "Mythology", "Trojan War", "No Sequel"))); getDs().createAggregation(Book.class) .match(getDs().getQueryFactory().createQuery(getDs()) .field("author").equal("Homer")) .group("author", grouping("copies", sum("copies"))) .out("testAverage", Author.class); DBCursor testAverage = getDb().getCollection("testAverage").find(); Assert.assertNotNull(testAverage); try { Assert.assertEquals(20, testAverage.next().get("copies")); } finally { testAverage.close(); } }
.find(getFilterForInitialImport(definition.getMongoCollectionFilter(), lastId)) .sort(new BasicDBObject("_id", 1)); while (cursor.hasNext() && context.getStatus() == Status.RUNNING) { DBObject object = cursor.next(); count++; if (cursor.hasNext()) { lastId = addInsertToStream(null, applyFieldFilter(object), collection.getName()); } else { while (cursor.hasNext()) { DBObject object = cursor.next(); if (object instanceof GridFSDBFile) { GridFSDBFile file = grid.findOne(new ObjectId(object.get(MongoDBRiver.MONGODB_ID_FIELD).toString()));
BasicDBObject query = new BasicDBObject("productFeature.id" : "Product1"); DBCursor cursor = coll.find(query).sort( new BasicDBObject("label", 1)); while (cursor.hasNext()){ System.out.println(cursor.next().get("id")); }
private List<Shard> getShards(boolean isMongos) { List<Shard> shards = new ArrayList<>(); if (isMongos) { try (DBCursor cursor = getConfigDb().getCollection("shards").find()) { while (cursor.hasNext()) { DBObject item = cursor.next(); List<ServerAddress> shardServers = getServerAddressForReplica(item); if (shardServers != null) { String shardName = item.get(MongoDBRiver.MONGODB_ID_FIELD).toString(); MongoClient shardClient = mongoClientService.getMongoShardClient(definition, shardServers); ensureIsReplicaSet(shardClient); Timestamp<?> latestOplogTimestamp = getCurrentOplogTimestamp(shardClient); shards.add(new Shard(shardName, shardServers, latestOplogTimestamp)); } } } return shards; } else { ensureIsReplicaSet(clusterClient); List<ServerAddress> servers = clusterClient.getServerAddressList(); Timestamp<?> latestOplogTimestamp = getCurrentOplogTimestamp(clusterClient); shards.add(new Shard("unsharded", servers, latestOplogTimestamp)); return shards; } }
DBObject allQuery = new BasicDBObject(); DBObject removeIdProjection = new basicDBObject("_id", 0); DBCollection collection = db.getCollection("volume"); DBCursor cursor = collection.find(allQuery, removeIdProjection); DBObject resultElement = cursor.next(); Map resultElementMap = resultElement.toMap(); System.out.println(resultElementMap);
@Override public K fetchOne() throws NonUniqueResultException { try { Long limit = queryMixin.getMetadata().getModifiers().getLimit(); if (limit == null) { limit = 2L; } DBCursor c = createCursor().limit(limit.intValue()); if (c.hasNext()) { K rv = transformer.apply(c.next()); if (c.hasNext()) { throw new NonUniqueResultException(); } return rv; } else { return null; } } catch (NoResults ex) { return null; } }
@Override public List<String> typeNames() { DBCursor cursor = collection.find( new BasicDBObject(), new BasicDBObject(FeatureTypeDBObject.KEY_typeName, 1)); List<String> typeNames = new ArrayList<String>(cursor.count()); while (cursor.hasNext()) { DBObject document = cursor.next(); if (document != null) { Object typeName = document.get(FeatureTypeDBObject.KEY_typeName); if (typeName instanceof String) { typeNames.add((String) typeName); } } } return typeNames; }
BasicDBObject query1; DBCursor cursor; query1 = new BasicDBObject("age", new BasicDBObject("$gt", 25)); cursor = incollection.find(query1); while(cursor.hasNext()){ System.out.println(cursor.next().get("username")); }