List of usage examples for com.mongodb DBCollection createIndex
public void createIndex(final DBObject keys)
From source file:org.graylog2.database.MongoConnection.java
License:Open Source License
/** * Get the message_counts collection. Lazily checks if correct indices are set. * * @return The messages collection/*from w w w . ja v a 2 s. c o m*/ */ public DBCollection getMessageCountsColl() { if (this.messageCountsCollection != null) { return this.messageCountsCollection; } // Collection has not been cached yet. Do it now. DBCollection coll = getDatabase().getCollection("message_counts"); coll.createIndex(new BasicDBObject("timestamp", 1)); this.messageCountsCollection = coll; return coll; }
From source file:org.graylog2.events.ClusterEventPeriodical.java
License:Open Source License
@VisibleForTesting static DBCollection prepareCollection(final MongoConnection mongoConnection) { final DB db = mongoConnection.getDatabase(); DBCollection coll = db.getCollection(COLLECTION_NAME); if (coll.isCapped()) { LOG.warn(/*w ww.j ava2 s . c o m*/ "The \"{}\" collection in MongoDB is capped which will cause problems. Please drop the collection.", COLLECTION_NAME); } coll.createIndex(DBSort.asc("timestamp").asc("producer").asc("consumers")); coll.setWriteConcern(WriteConcern.FSYNCED); return coll; }
From source file:org.i3xx.step.clockmongo.service.impl.ClockPersistenceServiceImpl.java
License:Apache License
/** * @param nspc The namespace//from ww w .j av a2 s. c o m * @param symbol The symbol * @return The DBCollection */ private DBCollection ensureCollection(String nspc, String symbol) { DBCollection col = null; if (db.collectionExists(nspc)) { col = db.getCollection(nspc); } else { col = db.getCollection(nspc); col.createIndex(new BasicDBObject("symbol", ASC)); } //fi return col; }
From source file:org.qi4j.entitystore.mongodb.MongoMapEntityStoreMixin.java
License:Apache License
@Override public void activateService() throws Exception { loadConfiguration();/*from ww w . j av a2 s.co m*/ // Create Mongo driver and open the database mongo = new Mongo(serverAddresses); db = mongo.getDB(databaseName); // Authenticate if needed if (!username.isEmpty()) { if (!db.authenticate(username, password)) { LOGGER.warn("Authentication against MongoDB with username '" + username + "' failed. Subsequent requests will be made 'anonymously'."); } } // Create index if needed db.requestStart(); DBCollection entities = db.getCollection(collectionName); if (entities.getIndexInfo().isEmpty()) { entities.createIndex(new BasicDBObject(IDENTITY_COLUMN, 1)); } db.requestDone(); }
From source file:parlare.application.server.model.Database.java
private String doClientMongo() { String print = ""; System.out.println("User:" + user + " Source:" + source + " Password:" + password); try {//from w w w. j av a 2s .co m // connect to the local database server MongoClient mongoClient = new MongoClient(new ServerAddress(server), Arrays.asList(MongoCredential.createMongoCRCredential(user, source, password.toCharArray())), new MongoClientOptions.Builder().build()); // get handle to "mydb" DB db = mongoClient.getDB("html5apps"); // Authenticate - optional // boolean auth = db.authenticate("foo", "bar"); // get a list of the collections in this database and print them out Set<String> collectionNames = db.getCollectionNames(); for (String s : collectionNames) { System.out.println(s); } // get a collection object to work with DBCollection testCollection = db.getCollection("testCollection"); // drop all the data in it testCollection.drop(); // make a document and insert it BasicDBObject doc = new BasicDBObject("name", "MongoDB").append("type", "database").append("count", 1) .append("info", new BasicDBObject("x", 203).append("y", 102)); testCollection.insert(doc); // get it (since it's the only one in there since we dropped the rest earlier on) DBObject myDoc = testCollection.findOne(); System.out.println(myDoc); // now, lets add lots of little documents to the collection so we can explore queries and cursors for (int i = 0; i < 100; i++) { testCollection.insert(new BasicDBObject().append("i", i)); } System.out.println("total # of documents after inserting 100 small ones (should be 101) " + testCollection.getCount()); // lets get all the documents in the collection and print them out DBCursor cursor = testCollection.find(); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } // now use a query to get 1 document out BasicDBObject query = new BasicDBObject("i", 71); cursor = testCollection.find(query); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } // now use a range query to get a larger subset query = new BasicDBObject("i", new BasicDBObject("$gt", 50)); // i.e. find all where i > 50 cursor = testCollection.find(query); try { while (cursor.hasNext()) { System.out.println("Cursor: " + cursor.next()); } } finally { cursor.close(); } // range query with multiple constraints query = new BasicDBObject("i", new BasicDBObject("$gt", 20).append("$lte", 30)); // i.e. 20 < i <= 30 cursor = testCollection.find(query); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } // create an index on the "i" field testCollection.createIndex(new BasicDBObject("i", 1)); // create index on "i", ascending // list the indexes on the collection List<DBObject> list = testCollection.getIndexInfo(); for (DBObject o : list) { System.out.println(o); } // See if the last operation had an error System.out.println("Last error : " + db.getLastError()); // see if any previous operation had an error System.out.println("Previous error : " + db.getPreviousError()); // force an error db.forceError(); // See if the last operation had an error System.out.println("Last error : " + db.getLastError()); db.resetError(); // release resources mongoClient.close(); } catch (UnknownHostException ex) { Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex); } return print; }
From source file:serwlety.szukaj.DaneSzukaj.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request//from w w w .jav a 2 s . c o m * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); DBCollection db = baza.FabrykaPolaczenia.getInstance().getConnection().getCollection("Osoba"); db.createIndex(new BasicDBObject("$**", "text")); DBObject d = QueryBuilder.start().text(request.getParameter("slowa")).get(); DBCursor cursor = db.find(d); List<DBObject> wyniki = new ArrayList<>(); while (cursor.hasNext()) { wyniki.add(cursor.next()); } db.dropIndexes(); request.setAttribute("wyniki", wyniki); String slowa = request.getParameter("slowa"); request.setAttribute("slowa", request.getParameter("slowa")); request.getRequestDispatcher("/WEB-INF/szukaj/daneWyniki.jsp").forward(request, response); }
From source file:tango.mongo.ImageManager.java
License:Open Source License
public ImageManager(MongoConnector mongo, DB project) { this.mongo = mongo; this.project = project; ArrayList<ObjectId> exps = mongo.getExperimentIds(); this.gfsField = new HashMap<ObjectId, GridFS>(exps.size()); this.gfsNucleus = new HashMap<ObjectId, GridFS>(exps.size()); gfsFieldThumbnail = new GridFS(project, "fieldThumbnail"); gfsNucleusThumbnail = new GridFS(project, "nucleusThumbnail"); DBCollection fieldsFilesT = project.getCollection("fieldThumbnail.files"); fieldsFilesT.createIndex(new BasicDBObject("field_id", 1)); DBCollection nucleiFilesT = project.getCollection("nucleusThumbnail.files"); nucleiFilesT.createIndex(new BasicDBObject("nucleus_id", 1).append("fileRank", 1)); for (ObjectId xp : exps) addExperiment(xp);/*from w ww. ja va 2s . c o m*/ }
From source file:tango.mongo.ImageManager.java
License:Open Source License
private void addExperiment(ObjectId id) { if (!gfsField.containsKey(id)) { String collectionName = "fieldImages_" + id.toHexString(); if (project.collectionExists(collectionName + ".files") || !project.collectionExists("field")) { gfsField.put(id, new GridFS(project, collectionName)); DBCollection fieldsFiles = project.getCollection(collectionName + ".files"); fieldsFiles.createIndex(new BasicDBObject("field_id", 1).append("fileRank", 1)); } else if (gfsFieldAll == null) {// retrocompatibilit gfsFieldAll = new GridFS(project, "field"); DBCollection fieldsFiles = project.getCollection("field.files"); fieldsFiles.createIndex(new BasicDBObject("field_id", 1).append("fileRank", 1)); gfsField.put(id, gfsFieldAll); }/*w w w.j ava2 s . c o m*/ } if (!gfsNucleus.containsKey(id)) { String collectionName = "nucleusImages_" + id.toHexString(); if (project.collectionExists(collectionName + ".files") || !project.collectionExists("nucleus")) { gfsNucleus.put(id, new GridFS(project, collectionName)); DBCollection nucleiFiles = project.getCollection(collectionName + ".files"); nucleiFiles .createIndex(new BasicDBObject("nucleus_id", 1).append("fileIdx", 1).append("fileType", 1)); } else if (gfsNucleusAll == null) {// retrocompatibilit gfsNucleusAll = new GridFS(project, "nucleus"); DBCollection nucleiFiles = project.getCollection("nucleus.files"); nucleiFiles .createIndex(new BasicDBObject("nucleus_id", 1).append("fileIdx", 1).append("fileType", 1)); gfsNucleus.put(id, gfsNucleusAll); } } }
From source file:tango.mongo.MongoConnector.java
License:Open Source License
public void ensureIndexes() { experiment.createIndex(new BasicDBObject("name", 1)); nucleusSettings.createIndex(new BasicDBObject("name", 1)); channelSettings.createIndex(new BasicDBObject("name", 1)); field.createIndex(new BasicDBObject("experiment_id", 1).append("name", 1)); nucleus.createIndex(new BasicDBObject("field_id", 1).append("idx", 1)); nucleus.createIndex(new BasicDBObject("experiment_id", 1)); structureMeasurement.createIndex(new BasicDBObject("nucleus_id", 1).append("structures", 1)); object3D.createIndex(new BasicDBObject("nucleus_id", 1).append("channelIdx", 1).append("idx", 1)); object3D.createIndex(new BasicDBObject("experiment_id", 1).append("channelIdx", 1).append("idx", 1)); DBCollection fieldsFiles = project.getCollection("field.files"); fieldsFiles.createIndex(new BasicDBObject("field_id", 1).append("fileRank", 1)); DBCollection nucleiFiles = project.getCollection("nucleus.files"); nucleiFiles.createIndex(new BasicDBObject("nucleus_id", 1).append("fileIdx", 1).append("fileType", 1)); DBCollection fieldsFilesT = project.getCollection("fieldThumbnail.files"); fieldsFilesT.createIndex(new BasicDBObject("field_id", 1)); DBCollection nucleiFilesT = project.getCollection("nucleusThumbnail.files"); nucleiFilesT.createIndex(new BasicDBObject("nucleus_id", 1).append("fileRank", 1)); selection.createIndex(new BasicDBObject("experiment_id", 1).append("name", 1)); }
From source file:test.mongodb.servlet.MongoDBServlet.java
License:Open Source License
protected void tutorial(PrintWriter pw) { try {//from w ww .j ava 2 s. c o m mongoDB.requestStart(); pw.format("<h1>Tutorial Objects Added to DB</h1>\n"); DBCollection coll = mongoDB.getCollection("testCollection"); BasicDBObject doc = new BasicDBObject(); doc.put("name", "MongoDB"); doc.put("type", "database"); doc.put("count", 1); BasicDBObject info = new BasicDBObject(); info.put("x", 203); info.put("y", 102); doc.put("info", info); coll.insert(doc); DBObject myDoc = coll.findOne(); pw.format("<h2>testCollection</h2><pre>%s</pre>\n", myDoc.toString()); for (int i = 0; i < 100; i++) { coll.insert(new BasicDBObject().append("i", i)); } BasicDBObject query = new BasicDBObject(); query.put("i", 71); DBCursor cur = coll.find(query); pw.format("<h2>100 i objects, #71:</h2><pre>%s</pre>\n", cur.next()); coll.createIndex(new BasicDBObject("i", 1)); // create index on "i", ascending } finally { mongoDB.requestDone(); } }