List of usage examples for com.mongodb.client MongoCollection find
FindIterable<TDocument> find();
From source file:me.lucko.luckperms.common.storage.dao.mongodb.MongoDao.java
License:MIT License
@Override public Set<UUID> getUniqueUsers() { Set<UUID> uuids = new HashSet<>(); MongoCollection<Document> c = database.getCollection(prefix + "users"); try (MongoCursor<Document> cursor = c.find().iterator()) { while (cursor.hasNext()) { Document d = cursor.next(); uuids.add(d.get("_id", UUID.class)); }/*from w ww . j av a2 s . c o m*/ } return uuids; }
From source file:me.lucko.luckperms.common.storage.dao.mongodb.MongoDao.java
License:MIT License
@Override public List<HeldPermission<UUID>> getUsersWithPermission(String permission) { ImmutableList.Builder<HeldPermission<UUID>> held = ImmutableList.builder(); MongoCollection<Document> c = database.getCollection(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; }/*from w w w.ja va 2 s.c om*/ held.add(NodeHeldPermission.of(holder, e)); } } } return held.build(); }
From source file:me.lucko.luckperms.common.storage.dao.mongodb.MongoDao.java
License:MIT License
@Override public boolean loadAllGroups() { List<String> groups = new ArrayList<>(); MongoCollection<Document> c = database.getCollection(prefix + "groups"); try (MongoCursor<Document> cursor = c.find().iterator()) { while (cursor.hasNext()) { String name = cursor.next().getString("_id"); groups.add(name);/*ww w. j a v a2 s . c om*/ } } boolean success = true; for (String g : groups) { try { loadGroup(g); } catch (Exception e) { e.printStackTrace(); success = false; } } if (!success) { throw new RuntimeException("Exception occurred whilst loading a group"); } GroupManager gm = plugin.getGroupManager(); gm.getAll().values().stream().filter(g -> !groups.contains(g.getName())).forEach(gm::unload); return true; }
From source file:me.lucko.luckperms.common.storage.dao.mongodb.MongoDao.java
License:MIT License
@Override public List<HeldPermission<String>> getGroupsWithPermission(String permission) { ImmutableList.Builder<HeldPermission<String>> held = ImmutableList.builder(); MongoCollection<Document> c = database.getCollection(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; }/*from w ww . j a va2 s . c o m*/ held.add(NodeHeldPermission.of(holder, e)); } } } return held.build(); }
From source file:me.lucko.luckperms.common.storage.dao.mongodb.MongoDao.java
License:MIT License
@Override public boolean loadAllTracks() { List<String> tracks = new ArrayList<>(); MongoCollection<Document> c = database.getCollection(prefix + "tracks"); try (MongoCursor<Document> cursor = c.find().iterator()) { while (cursor.hasNext()) { String name = cursor.next().getString("_id"); tracks.add(name);//from w w w . j av a 2 s .c o m } } boolean success = true; for (String t : tracks) { try { loadTrack(t); } catch (Exception e) { e.printStackTrace(); success = false; } } if (!success) { throw new RuntimeException("Exception occurred whilst loading a track"); } TrackManager tm = plugin.getTrackManager(); tm.getAll().values().stream().filter(t -> !tracks.contains(t.getName())).forEach(tm::unload); return true; }
From source file:module.script.epimed_ontology.AddEpimedGroupToSamples.java
License:Open Source License
public AddEpimedGroupToSamples() { // ===== Session PostgreSQL ===== SessionFactory sessionFactory = HibernateUtil .buildSessionFactory("config/epimed_semantic.hibernate.cfg.xml"); Session session = sessionFactory.openSession(); ClTopologyDao topologyDao = new ClTopologyDao(session); // ===== Session Mongo ===== MongoClient mongoClient = MongoUtil.buildMongoClient(); MongoDatabase db = mongoClient.getDatabase("epimed_experiments"); MongoCollection<Document> collectionSample = db.getCollection("sample"); List<Document> samples = collectionSample.find().into(new ArrayList<Document>()); for (int i = 0; i < samples.size(); i++) { Document sample = samples.get(i); Document expgroup = sample.get("exp_group", Document.class); String idTopology = expgroup.getString("id_topology"); if (idTopology != null && !idTopology.isEmpty()) { ClTopology topology = topologyDao.find(idTopology); ClEpimedGroup grp1 = topology.getClEpimedGroup(); ClEpimedGroup grp2 = grp1.getParent(); ClEpimedGroup grp3 = grp2.getParent(); expgroup.append("tissue_group_level1", grp1.getName()); expgroup.append("tissue_group_level2", grp2.getName()); expgroup.append("tissue_group_level3", grp3.getName()); System.out.println((i + 1) + "/" + samples.size() + " " + expgroup); sample.append("exp_group", expgroup); collectionSample.updateOne(Filters.eq("_id", sample.getString("_id")), new Document("$set", sample)); }//from ww w. jav a 2s . com } // === Commit transaction === // session.getTransaction().commit(); session.getTransaction().rollback(); if (session.isOpen()) { session.close(); } sessionFactory.close(); mongoClient.close(); }
From source file:module.UpdateNumberSamples.java
License:Open Source License
public UpdateNumberSamples() { // ===== Connection ===== MongoClient mongoClient = MongoUtil.buildMongoClient(); MongoDatabase db = mongoClient.getDatabase("epimed_experiments"); MongoCollection<Document> collectionSeries = db.getCollection("series"); MongoCollection<Document> collectionSamples = db.getCollection("sample"); // String [] listIdSeries = {"TISSUE_SPECIFIC_GENES_MM"}; // List<Document> listSeries = collectionSeries.find(Filters.in("_id", listIdSeries)).into(new ArrayList<Document>()); List<Document> listSeries = collectionSeries.find().into(new ArrayList<Document>()); for (Document ser : listSeries) { System.out.println(ser);/*from w w w . ja v a 2s.co m*/ String idSeries = ser.getString("_id"); Bson filter = Filters.in("series", idSeries); Long nbSamples = collectionSamples.count(filter); System.out.println(idSeries + " " + nbSamples); ser.append("nb_samples", nbSamples); collectionSeries.updateOne(Filters.eq("_id", idSeries), new Document("$set", ser)); } mongoClient.close(); }
From source file:module.UpdateTissueStatus.java
License:Open Source License
public UpdateTissueStatus() { // ===== Connection ===== MongoClient mongoClient = MongoUtil.buildMongoClient(); MongoDatabase db = mongoClient.getDatabase("epimed_experiments"); MongoCollection<Document> collectionSample = db.getCollection("sample"); List<Document> samples = collectionSample.find().into(new ArrayList<Document>()); ;//from w ww. ja v a 2s . c om for (Document sample : samples) { Document expgroup = sample.get("exp_group", Document.class); Integer idTissueStatus = expgroup.getInteger("id_tissue_status"); if (idTissueStatus != null) { if (idTissueStatus.equals(1)) { expgroup.put("tissue_status", "normal"); } if (idTissueStatus.equals(2)) { expgroup.put("tissue_status", "pathological_non_tumoral"); } if (idTissueStatus.equals(3)) { expgroup.put("tissue_status", "tumoral"); } sample.put("exp_group", expgroup); System.out.println(sample.get("exp_group", Document.class)); collectionSample.updateOne(Filters.eq("_id", sample.getString("_id")), new Document("$set", sample)); } } mongoClient.close(); }
From source file:mongodb.QuickTour.java
License:Apache License
/** * Run this main method to see the output of this quick example. * * @param args takes an optional single argument for the connection string *//* w w w . ja v a 2s . co m*/ public static void main(final String[] args) { //represents a pool of connections to the database MongoClient mongoClient = new MongoClient("10.9.17.105", 27017); // get handle to "mydb" database MongoDatabase database = mongoClient.getDatabase("test"); // get a handle to the "test" collection MongoCollection<Document> collection = database.getCollection("test"); // drop all the data in it collection.drop(); // make a document and insert it Document doc = new Document("name", "MongoDB").append("type", "database").append("count", 1).append("info", new Document("x", 203).append("y", 102)); collection.insertOne(doc); // get it (since it's the only one in there since we dropped the rest earlier on) Document myDoc = collection.find().first(); System.out.println(myDoc.toJson()); // now, lets add lots of little documents to the collection so we can explore queries and cursors List<Document> documents = new ArrayList<Document>(); for (int i = 0; i < 100; i++) { documents.add(new Document("i", i)); } collection.insertMany(documents); System.out.println( "total # of documents after inserting 100 small ones (should be 101) " + collection.count()); // find first myDoc = collection.find().first(); System.out.println(myDoc.toJson()); // lets get all the documents in the collection and print them out MongoCursor<Document> cursor = collection.find().iterator(); try { while (cursor.hasNext()) { System.out.println(cursor.next().toJson()); } } finally { cursor.close(); } for (Document cur : collection.find()) { System.out.println(cur.toJson()); } // now use a query to get 1 document out myDoc = collection.find(eq("i", 71)).first(); System.out.println(myDoc.toJson()); // now use a range query to get a larger subset cursor = collection.find(gt("i", 50)).iterator(); try { while (cursor.hasNext()) { System.out.println(cursor.next().toJson()); } } finally { cursor.close(); } // range query with multiple constraints cursor = collection.find(and(gt("i", 50), lte("i", 100))).iterator(); try { while (cursor.hasNext()) { System.out.println(cursor.next().toJson()); } } finally { cursor.close(); } // Query Filters myDoc = collection.find(eq("i", 71)).first(); System.out.println(myDoc.toJson()); // now use a range query to get a larger subset Block<Document> printBlock = new Block<Document>() { @Override public void apply(final Document document) { System.out.println(document.toJson()); } }; collection.find(gt("i", 50)).forEach(printBlock); // filter where; 50 < i <= 100 collection.find(and(gt("i", 50), lte("i", 100))).forEach(printBlock); // Sorting myDoc = collection.find(exists("i")).sort(descending("i")).first(); System.out.println(myDoc.toJson()); // Projection myDoc = collection.find().projection(excludeId()).first(); System.out.println(myDoc.toJson()); // Update One collection.updateOne(eq("i", 10), new Document("$set", new Document("i", 110))); // Update Many UpdateResult updateResult = collection.updateMany(lt("i", 100), new Document("$inc", new Document("i", 100))); System.out.println(updateResult.getModifiedCount()); // Delete One collection.deleteOne(eq("i", 110)); // Delete Many DeleteResult deleteResult = collection.deleteMany(gte("i", 100)); System.out.println(deleteResult.getDeletedCount()); collection.drop(); // ordered bulk writes List<WriteModel<Document>> writes = new ArrayList<WriteModel<Document>>(); writes.add(new InsertOneModel<Document>(new Document("_id", 4))); writes.add(new InsertOneModel<Document>(new Document("_id", 5))); writes.add(new InsertOneModel<Document>(new Document("_id", 6))); writes.add( new UpdateOneModel<Document>(new Document("_id", 1), new Document("$set", new Document("x", 2)))); writes.add(new DeleteOneModel<Document>(new Document("_id", 2))); writes.add(new ReplaceOneModel<Document>(new Document("_id", 3), new Document("_id", 3).append("x", 4))); collection.bulkWrite(writes); collection.drop(); collection.bulkWrite(writes, new BulkWriteOptions().ordered(false)); collection.find().forEach(printBlock); // Clean up // database.drop(); // release resources mongoClient.close(); }
From source file:mongodbmanipulation.MongoDBManipulation.java
/** * @param args the command line arguments *//*www . j av a 2s . c o m*/ public static void main(String[] args) { // TODO code application logic here UserInterface.main(args); try { MongoClient mongoClient = new MongoClient("localhost", 27017); MongoDatabase db = mongoClient.getDatabase("test"); System.out.println("Connect to database successfully"); // boolean auth = db.authenticate(myUserName, myPassword); // System.out.println("Authentication: "+auth); MongoCollection<Document> coll = db.getCollection("animals"); FindIterable<Document> cursor = coll.find(); int i = 0; for (Iterator iterator = cursor.iterator(); iterator.hasNext(); i++) { System.out.println(iterator.next()); if (i == 100) break; } } catch (Exception e) { System.out.println("Failed!"); } }