Refine search
/** * Select all documents in collection and get a cursor to the selected documents. * * @return A cursor to the documents that match the query criteria * @mongodb.driver.manual tutorial/query-documents/ Querying */ public DBCursor find() { return find(new BasicDBObject()); }
private Timestamp<?> getCurrentOplogTimestamp() { try (DBCursor cursor = oplogCollection.find().sort(new BasicDBObject(MongoDBRiver.INSERTION_ORDER_KEY, -1)).limit(1)) { return Timestamp.on(cursor.next()); } }
/** * 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()); }
DBCollection dbCollection = db.getCollection("mycollection"); BasicDBObject dbObject = new BasicDBObject(); dbObject.put("_id", "3"); // your update condition - or the query DBObject newObject = dbCollection.find(dbObject).toArray().get(0); // I just take the first element. Can iterate through as per your requirement if multiple fields exist newObject.put("key","value"); //add field, either a new field or any existing field dbCollection.findAndModify(dbObject, newObject);
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());
@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; }
@Override public Map<String, Long> totalCountByType() { final Map<String, Long> inputCountByType = new HashMap<>(); try (DBCursor inputTypes = dbCollection.find(null, new BasicDBObject(MessageInput.FIELD_TYPE, 1))) { for (DBObject inputType : inputTypes) { final String type = (String) inputType.get(MessageInput.FIELD_TYPE); if (type != null) { final Long oldValue = inputCountByType.get(type); final Long newValue = (oldValue == null) ? 1 : oldValue + 1; inputCountByType.put(type, newValue); } } } return inputCountByType; }
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(); }
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()));
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 Map<String, Long> countByType() { final Map<String, Long> outputsCountByType = new HashMap<>(); try (DBCursor outputTypes = dbCollection.find(null, new BasicDBObject(OutputImpl.FIELD_TYPE, 1))) { for (DBObject outputType : outputTypes) { final String type = (String) outputType.get(OutputImpl.FIELD_TYPE); if (type != null) { final Long oldValue = outputsCountByType.get(type); final Long newValue = (oldValue == null) ? 1 : oldValue + 1; outputsCountByType.put(type, newValue); } } } return outputsCountByType; }
@Override public boolean exists(ObjectId id) { DBObject query = new BasicDBObject(); query.put("oid", id.toString()); return collection.find(query).hasNext(); }
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 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; }