List of usage examples for com.mongodb.client MongoCursor close
@Override
void close();
From source file:com.glaf.core.config.mongodb.MongodbConfig.java
License:Apache License
public Object get(Object key) { BasicDBObject filter = new BasicDBObject(); filter.put("key", key); MongoCursor<Document> cur = dbCollection.find(filter).iterator(); long now = System.currentTimeMillis(); Object value = null;// w w w . ja va 2 s . c o m try { while (cur.hasNext()) { Document doc = cur.next(); value = doc.get(key.toString()); long saveTime = (Long) doc.get("time"); if ((now - saveTime) > expireMinutes * 60000) { /** * ??? */ dbCollection.deleteOne(doc); } } } finally { cur.close(); } return value; }
From source file:com.glaf.core.resource.mongodb.MongodbResource.java
License:Apache License
public Object getObject(Object key) { BasicDBObject filter = new BasicDBObject(); filter.put("key", key); MongoCursor<Document> cur = dbCollection.find(filter).iterator(); long now = System.currentTimeMillis(); Object value = null;/* www .j av a 2s . c o m*/ try { while (cur.hasNext()) { Document doc = cur.next(); value = doc.get(key.toString()); long saveTime = (Long) doc.get("time"); if ((now - saveTime) > expireMinutes * 60000) { /** * ??? */ dbCollection.deleteOne(filter); } } } finally { cur.close(); } return value; }
From source file:com.glaf.j2cache.mongodb.MongodbCache.java
License:Apache License
public Object get(Object key) throws CacheException { BasicDBObject filter = new BasicDBObject(); filter.put("key", key); MongoCursor<Document> cur = dbCollection.find(filter).iterator(); long now = System.currentTimeMillis(); Object value = null;//from ww w . j a v a 2 s . c o m try { while (cur.hasNext()) { Document doc = cur.next(); value = doc.get(key.toString()); long saveTime = (Long) doc.get("time"); if ((now - saveTime) > expireMinutes * 60000) { /** * ? */ dbCollection.deleteOne(filter); } } } finally { cur.close(); } return value; }
From source file:com.helion3.prism.storage.mongodb.MongoRecords.java
License:MIT License
@Override public CompletableFuture<List<Result>> query(QuerySession session, boolean translate) throws Exception { Query query = session.getQuery(); checkNotNull(query);/*from w w w . j a v a 2 s .co m*/ // Prepare results List<Result> results = new ArrayList<Result>(); CompletableFuture<List<Result>> future = new CompletableFuture<List<Result>>(); // Get collection MongoCollection<Document> collection = MongoStorageAdapter .getCollection(MongoStorageAdapter.collectionEventRecordsName); // Append all conditions Document matcher = new Document("$match", buildConditions(query.getConditions())); // Session configs int sortDir = 1; // @todo needs implementation boolean shouldGroup = query.isAggregate(); // Sorting Document sortFields = new Document(); sortFields.put(DataQueries.Created.toString(), sortDir); sortFields.put(DataQueries.Y.toString(), 1); sortFields.put(DataQueries.X.toString(), 1); sortFields.put(DataQueries.Z.toString(), 1); Document sorter = new Document("$sort", sortFields); // Offset/Limit Document limit = new Document("$limit", query.getLimit()); // Build aggregators AggregateIterable<Document> aggregated = null; if (shouldGroup) { // Grouping fields Document groupFields = new Document(); groupFields.put(DataQueries.EventName.toString(), "$" + DataQueries.EventName); groupFields.put(DataQueries.Player.toString(), "$" + DataQueries.Player); groupFields.put(DataQueries.Cause.toString(), "$" + DataQueries.Cause); groupFields.put(DataQueries.Target.toString(), "$" + DataQueries.Target); // Entity groupFields.put(DataQueries.Entity.toString(), "$" + DataQueries.Entity.then(DataQueries.EntityType)); // Day groupFields.put("dayOfMonth", new Document("$dayOfMonth", "$" + DataQueries.Created)); groupFields.put("month", new Document("$month", "$" + DataQueries.Created)); groupFields.put("year", new Document("$year", "$" + DataQueries.Created)); Document groupHolder = new Document("_id", groupFields); groupHolder.put(DataQueries.Count.toString(), new Document("$sum", 1)); Document group = new Document("$group", groupHolder); // Aggregation pipeline List<Document> pipeline = new ArrayList<Document>(); pipeline.add(matcher); pipeline.add(group); pipeline.add(sorter); pipeline.add(limit); aggregated = collection.aggregate(pipeline); Prism.getLogger().debug("MongoDB Query: " + pipeline); } else { // Aggregation pipeline List<Document> pipeline = new ArrayList<Document>(); pipeline.add(matcher); pipeline.add(sorter); pipeline.add(limit); aggregated = collection.aggregate(pipeline); Prism.getLogger().debug("MongoDB Query: " + pipeline); } session.getCommandSource().get() .sendMessage(Format.subduedHeading("Query completed, building snapshots...")); // Iterate results and build our event record list MongoCursor<Document> cursor = aggregated.iterator(); try { List<UUID> uuidsPendingLookup = new ArrayList<UUID>(); while (cursor.hasNext()) { // Mongo document Document wrapper = cursor.next(); Document document = shouldGroup ? (Document) wrapper.get("_id") : wrapper; DataContainer data = documentToDataContainer(document); if (shouldGroup) { data.set(DataQueries.Count, wrapper.get(DataQueries.Count.toString())); } // Build our result object Result result = Result.from(wrapper.getString(DataQueries.EventName.toString()), session.getQuery().isAggregate()); // Determine the final name of the event source if (document.containsKey(DataQueries.Player.toString())) { String uuid = document.getString(DataQueries.Player.toString()); data.set(DataQueries.Cause, uuid); if (translate) { uuidsPendingLookup.add(UUID.fromString(uuid)); } } else { data.set(DataQueries.Cause, document.getString(DataQueries.Cause.toString())); } result.data = data; results.add(result); } if (translate && !uuidsPendingLookup.isEmpty()) { DataUtil.translateUuidsToNames(results, uuidsPendingLookup).thenAccept(finalResults -> { future.complete(finalResults); }); } else { future.complete(results); } } finally { cursor.close(); } return future; }
From source file:com.hurence.logisland.service.mongodb.MongoDBControllerService.java
License:Apache License
@Override public Document findOne(Document query) { MongoCursor<Document> cursor = this.col.find(query).limit(1).iterator(); Document retVal = cursor.tryNext(); cursor.close(); return retVal; }
From source file:com.hurence.logisland.service.mongodb.MongoDBControllerService.java
License:Apache License
@Override public Document findOne(Document query, Document projection) { MongoCursor<Document> cursor = projection != null ? this.col.find(query).projection(projection).limit(1).iterator() : this.col.find(query).limit(1).iterator(); Document retVal = cursor.tryNext(); cursor.close(); return retVal; }
From source file:com.hurence.logisland.service.mongodb.MongoDBControllerService.java
License:Apache License
@Override public List<Document> findMany(Document query, Document sort, int limit) { FindIterable<Document> fi = this.col.find(query); if (limit > 0) { fi = fi.limit(limit);/* ww w .j a va2 s.c o m*/ } if (sort != null) { fi = fi.sort(sort); } MongoCursor<Document> cursor = fi.iterator(); List<Document> retVal = new ArrayList<>(); while (cursor.hasNext()) { retVal.add(cursor.next()); } cursor.close(); return retVal; }
From source file:com.imos.sample.SampleMongoDB.java
public static void main(String[] args) { MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017"); MongoClient mongoClient = new MongoClient(connectionString); MongoDatabase database = mongoClient.getDatabase("sampledb"); MongoCollection<Document> collection = database.getCollection("sampleLog"); System.out.println(collection.count()); MongoCursor<Document> cursor = collection.find().iterator(); ObjectMapper mapper = new ObjectMapper(); try {//from w w w .j a v a 2 s .co m while (cursor.hasNext()) { try { System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(cursor.next())); } catch (JsonProcessingException ex) { Logger.getLogger(SampleMongoDB.class.getName()).log(Level.SEVERE, null, ex); } } } finally { cursor.close(); } }
From source file:com.inflight.rest.ViewPlaces.java
@SuppressWarnings("resource") @GET// w ww .j a va 2 s . co m @Path("/all") @Produces(MediaType.APPLICATION_JSON) public String getLocations() { JSONArray jsonArr = new JSONArray(); MongoClientURI connectionString = new MongoClientURI( "mongodb://ramkrish:1234567@ds029446.mlab.com:29446/inflight"); MongoClient mongoClient = new MongoClient(connectionString); MongoDatabase database = mongoClient.getDatabase("inflight"); MongoCollection<Document> collection = database.getCollection("location"); MongoCursor<Document> cursor = collection.find().iterator(); try { while (cursor.hasNext()) { jsonArr.put(cursor.next()); } } finally { cursor.close(); } return jsonArr.toString(); }
From source file:com.mycompany.mavenproject2.AddBrandController.java
public void InsertMongo() throws UnknownHostException { //MongoInsert count = (int) col.count(); if (count == 0) { seedData = createSeedData();/*from ww w . j av a2 s .c o m*/ col.insertOne(seedData); } else { sort1 = new BasicDBObject(); MongoCursor<Document> cursor = col.find().sort(sort1).limit(1).skip((int) count - 1).iterator(); try { while (cursor.hasNext()) { ID = cursor.next().getInteger("ID"); System.out.println("last ID " + ID); ID++; System.out.println("Inserted " + ID); } } finally { cursor.close(); } final Document seedData = createSeedData(); col.insertOne(seedData); } for (Map.Entry<String, Object> entry : seedData.entrySet()) { System.out.println(entry.getKey() + "/" + entry.getValue()); } BrandName.clear(); ManName.clear(); }