Refine search
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")); }
/** * 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); }
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"));
@Test public void save() { getMorphia().map(Class1.class, Class2.class); final Class2 class2 = new Class2(); class2.setId(new ObjectId()); class2.setText("hello world"); getDs().save(class2); final BasicDBObject query = new BasicDBObject("_id", class2.getId()); Assert.assertFalse(getDs().getCollection(Class1.class).find(query).hasNext()); Assert.assertTrue(getDs().getCollection(Class2.class).find(query).hasNext()); }
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; }
BasicDBObject query = new BasicDBObject(); query.put("name", "user"); query.put("password", "[YOUR ENCRYPTED PASSWORD HERE]"); DBCollection collection = db.getCollection("yourcollectionname"); DBCursor cursor = collection.find(query); while (cursor.hasNext()) { //do something with cursor.next(); }
.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())); if (cursor.hasNext()) { lastId = addInsertToStream(null, file); } else {
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; } }
@Override public boolean exists(ObjectId id) { DBObject query = new BasicDBObject(); query.put("oid", id.toString()); return collection.find(query).hasNext(); }
@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")); }
DBCollection coll = db.getCollection("test"); BasicDBObject query = new BasicDBObject("room", "Kitchen"); // optional, limit the fields to only have the lights field BasicDBObject fields = new BasicDBObject("lights",1).append("_id",false); DBCursor curs = coll.find(query, fields); while(curs.hasNext()) { DBObject o = curs.next(); // shows the whole result document System.out.println(o.toString()); BasicDBList lights = (BasicDBList) o.get("lights"); // shows the lights array -- this is actually a collection of DBObjects System.out.println(lights.toString()); // optional: break it into a native java array BasicDBObject[] lightArr = lights.toArray(new BasicDBObject[0]); for(BasicDBObject dbObj : lightArr) { // shows each item from the lights array System.out.println(dbObj); } }