List of usage examples for com.mongodb.client MongoCollection find
FindIterable<TDocument> find(ClientSession clientSession);
From source file:module.test.SearchPlatforms.java
License:Open Source License
public SearchPlatforms() { // ===== Connection ===== MongoClient mongoClient = MongoUtil.buildMongoClient(); MongoDatabase db = mongoClient.getDatabase("epimed_experiments"); MongoCollection<Document> collectionSamples = db.getCollection("sample"); MongoCollection<Document> collectionPlatforms = db.getCollection("platform"); List<Document> list = collectionSamples .aggregate(Arrays.asList(Aggregates.group("$exp_group.id_platform", Accumulators.sum("total", 1)), Aggregates.sort(Sorts.orderBy(Sorts.descending("total"))))) .into(new ArrayList<Document>()); for (int i = 0; i < list.size(); i++) { System.out.println((i + 1) + " " + list.get(i)); }// w ww . ja v a 2s . c o m // collectionPlatforms.find(Filters.regex("title", ".*ethyl.*")).forEach(printBlock); collectionPlatforms.find(Filters.eq("id_organism", "9606")).forEach(printBlock); mongoClient.close(); }
From source file:module.test.SearchSamples.java
License:Open Source License
public SearchSamples() { // ===== Connection ===== MongoClient mongoClient = MongoUtil.buildMongoClient(); MongoDatabase db = mongoClient.getDatabase("epimed_experiments"); MongoCollection<Document> collectionSamples = db.getCollection("samples"); List<Document> list = collectionSamples.find(Filters.and(Filters.eq("exp_group.id_tissue_stage", 4), Filters.ne("exp_group.main_gse_number", "GSE30654"))).into(new ArrayList<Document>()); for (Document document : list) { System.out.println(document); }/*from www . j a va2s. c o m*/ mongoClient.close(); }
From source file:module.test.SearchSeries.java
License:Open Source License
public SearchSeries() throws ParseException { // ===== Connection ===== MongoClient mongoClient = MongoUtil.buildMongoClient(); MongoDatabase db = mongoClient.getDatabase("epimed_experiments"); MongoCollection<Document> collectionSamples = db.getCollection("series"); String[] listSeries = { "E-MTAB-365", "GSE3156", "GSE3744", "GSE4922", "GSE5764", "GSE6367", "GSE6883", "GSE8977", "GSE9014", "GSE9195", "GSE10885", "GSE11078", "GSE11352", "GSE12080", "GSE12237", "GSE12276", "GSE14017", "GSE16391", "GSE17215", "GSE17907", "GSE19615", "GSE21653", "GSE28681", "GSE38867" }; List<Document> list = collectionSamples.find(Filters.in("_id", listSeries)).into(new ArrayList<Document>()); Set<String> setGse = new HashSet<String>(); List<String> listGse = new ArrayList<String>(); for (int i = 0; i < list.size(); i++) { Document doc = list.get(i); setGse.add(doc.getString("_id")); }//from ww w . ja v a 2 s . co m listGse.addAll(setGse); Collections.sort(listGse); System.out.println("Found " + list.size() + " samples"); System.out.println("List GSE " + listGse); mongoClient.close(); }
From source file:module.UpdatePlatformType.java
License:Open Source License
public UpdatePlatformType() { // ===== Connection ===== MongoClient mongoClient = MongoUtil.buildMongoClient(); MongoDatabase db = mongoClient.getDatabase("epimed_experiments"); MongoCollection<Document> collectionPlatforms = db.getCollection("platform"); MongoCollection<Document> collectionSeries = db.getCollection("series"); // ===== Platforms ===== Bson filters = Filters.eq("type", "unknown"); List<Document> listPlatforms = collectionPlatforms.find(filters).into(new ArrayList<Document>()); for (Document platform : listPlatforms) { System.out.println("--------------------------------------------------------------"); String idPlatform = platform.getString("_id"); System.out.println(platform); List<Document> listSeries = collectionSeries.find(Filters.in("platforms", idPlatform)) .into(new ArrayList<Document>()); for (Document series : listSeries) { System.out.println(" \t -> " + series.getString("_id") + " " + series.getString("title")); }/*from w w w .j a v a2 s . co m*/ // platform.append("type", "expression array"); // collectionPlatforms.updateOne(Filters.eq("_id", idPlatform), new Document("$set", platform)); } mongoClient.close(); }
From source file:mongodb.QuickTourAdmin.java
License:Apache License
/** * Run this main method to see the output of this quick example. * * @param args//from w ww. j av a 2 s .co m * takes an optional single argument for the connection string */ public static void main(final String[] args) { MongoClient mongoClient; if (args.length == 0) { // connect to the specified database server mongoClient = new MongoClient("10.9.17.105", 27017); } else { mongoClient = new MongoClient(new MongoClientURI(args[0])); } // get handle to "test" database MongoDatabase database = mongoClient.getDatabase("test"); database.drop(); // get a handle to the "test" collection MongoCollection<Document> collection = database.getCollection("test"); // drop all the data in it collection.drop(); // getting a list of databases for (String name : mongoClient.listDatabaseNames()) { System.out.println(name); } // drop a database mongoClient.getDatabase("databaseToBeDropped").drop(); // create a collection database.createCollection("cappedCollection", new CreateCollectionOptions().capped(true).sizeInBytes(0x100000)); for (String name : database.listCollectionNames()) { System.out.println(name); } // drop a collection: collection.drop(); // create an ascending index on the "i" field // 1 ascending or -1 for descending collection.createIndex(new Document("i", 1)); // list the indexes on the collection for (final Document index : collection.listIndexes()) { System.out.println(index.toJson()); } // create a text index on the "content" field // text indexes to support text search of string content collection.createIndex(new Document("content", "text")); collection.insertOne(new Document("_id", 0).append("content", "textual content")); collection.insertOne(new Document("_id", 1).append("content", "additional content")); collection.insertOne(new Document("_id", 2).append("content", "irrelevant content")); // Find using the text index long matchCount = collection.count(text("textual content -irrelevant")); System.out.println("Text search matches: " + matchCount); // Find using the $language operator Bson textSearch = text("textual content -irrelevant", "english"); matchCount = collection.count(textSearch); System.out.println("Text search matches (english): " + matchCount); // Find the highest scoring match Document projection = new Document("score", new Document("$meta", "textScore")); Document myDoc = collection.find(textSearch).projection(projection).first(); System.out.println("Highest scoring document: " + myDoc.toJson()); // Run a command Document buildInfo = database.runCommand(new Document("buildInfo", 1)); System.out.println(buildInfo); // release resources database.drop(); mongoClient.close(); }
From source file:mongotweet.MongoTweet.java
public static void tweet(String uname, String tweet) { Random rand = new Random(); String id = "" + rand.nextInt(99999999); Document doc = new Document().append("tweet_id", id).append("username", uname).append("body", tweet); insertDocument("tweets", doc); doc = new Document().append("tweet_id", id).append("username", uname).append("time", System.currentTimeMillis()); insertDocument("timeline", doc); insertDocument("userline", doc); System.out.println(uname + ":" + tweet + " published"); MongoCollection coll = db.getCollection("followers"); BsonDocument where = new BsonDocument().append("username", new BsonString(uname)); MongoCursor<Document> cursor = coll.find(where).iterator(); String username;/*from w w w . java 2 s .co m*/ while (cursor.hasNext()) { Document tmp = cursor.next(); username = (String) tmp.get("follower"); doc = new Document().append("tweet_id", id).append("username", username).append("time", System.currentTimeMillis()); insertDocument("timeline", doc); } }
From source file:mongotweet.MongoTweet.java
public static void showTweet(String uname) { MongoCollection coll = db.getCollection("tweets"); BsonDocument where = new BsonDocument().append("username", new BsonString(uname)); MongoCursor<Document> cursor = coll.find(where).iterator(); String username, body;/*from www .j a v a2s. c o m*/ while (cursor.hasNext()) { Document tmp = cursor.next(); username = (String) tmp.get("username"); body = (String) tmp.get("body"); System.out.format("%s : %s \n", username, body); } }
From source file:mongotweet.MongoTweet.java
public static void showTimeline(String uname) { MongoCollection coll = db.getCollection("timeline"); // MongoCursor<Document> cursor = coll.find().iterator(); BsonDocument where = new BsonDocument().append("username", new BsonString(uname)); MongoCursor<Document> cursor = coll.find(where).iterator(); String username, body, tweet_id; while (cursor.hasNext()) { tweet_id = (String) cursor.next().get("tweet_id"); BsonDocument where2 = new BsonDocument().append("tweet_id", new BsonString(tweet_id)); MongoCollection col2 = db.getCollection("tweets"); MongoCursor<Document> cursor2 = col2.find(where2).iterator(); while (cursor2.hasNext()) { Document tmp = cursor2.next(); username = (String) tmp.get("username"); body = (String) tmp.get("body"); System.out.format("%s : %s \n", username, body); }//from www. j a v a 2s.c o m } }
From source file:mongotwitter.MongoTwitter.java
public boolean signup(String username, String password) { MongoCollection<Document> users = db.getCollection("users"); Document oldDoc = users.find(eq("username", username)).first(); if (oldDoc != null) { System.out.println("* Signup failed : Username already exists"); return false; } else {/*from w w w. j ava 2s. c om*/ Document doc = new Document("username", username).append("password", password); users.insertOne(doc); System.out.println("* User @" + username + " is succesfully signed up"); //autologin nick = username; System.out.println("* Welcome to MongoTwitter, @" + username + "!"); return true; } }
From source file:mongotwitter.MongoTwitter.java
public boolean login(String username, String password) { MongoCollection<Document> users = db.getCollection("users"); Document oldDoc = users.find(and(eq("username", username), eq("password", password))).first(); if (oldDoc == null) { System.out.println("* Login failed : Either username does not exist or the password didn't match"); return false; } else {/*from ww w.ja v a 2 s.c om*/ nick = username; System.out.println("* Login succeed"); System.out.println("* Welcome back, @" + username + "!"); return true; } }