/** * Gets another database on same server * * @param name name of the database * @return the DB for the given name */ @SuppressWarnings("deprecation") // The old API (i.e. DB) will use deprecated methods. public DB getSisterDB(final String name) { return mongo.getDB(name); }
public MongoConnectionForTests(Mongo mongoClient, String dbName) { this.mongoClient = requireNonNull(mongoClient); this.db = mongoClient.getDB(dbName); this.mongoDatabase = null; }
Mongo mongo = PowerMockito.mock(Mongo.class); DB db = PowerMockito.mock(DB.class); DBCollection dbCollection = PowerMockito.mock(DBCollection.class); PowerMockito.when(mongo.getDB("foo")).thenReturn(db); PowerMockito.when(db.getCollection("bar")).thenReturn(dbCollection); MyService svc = new MyService(mongo); // Use some kind of dependency injection svc.getObjectById(1); PowerMockito.verify(dbCollection).findOne(new BasicDBObject("_id", 1));
/** * Drops the database if it exists. * * @param dbName name of database to drop * @throws MongoException if the operation fails */ public void dropDatabase(final String dbName) { getDB(dbName).dropDatabase(); }
/** * Returns the URI's DB object from a given Mongo instance. * * @param mongo the Mongo instance to get the database from. * @return the database specified in this URI */ @SuppressWarnings("deprecation") public DB connectDB(final Mongo mongo) { return mongo.getDB(getDatabaseNonNull()); }
/** * Forces the master server to fsync the RAM data to disk, then lock all writes. The database will be read-only after this command * returns. * * @return result of the command execution * @throws MongoException if there's a failure * @mongodb.driver.manual reference/command/fsync/ fsync command */ @Deprecated public CommandResult fsyncAndLock() { DBObject command = new BasicDBObject("fsync", 1); command.put("lock", 1); return getDB(ADMIN_DATABASE_NAME).command(command); }
/** * Forces the master server to fsync the RAM data to disk This is done automatically by the server at intervals, but can be forced for * better reliability. * * @param async if true, the fsync will be done asynchronously on the server. * @return result of the command execution * @throws MongoException if there's a failure * @mongodb.driver.manual reference/command/fsync/ fsync command */ @Deprecated public CommandResult fsync(final boolean async) { DBObject command = new BasicDBObject("fsync", 1); if (async) { command.put("async", 1); } return getDB(ADMIN_DATABASE_NAME).command(command); }
@SuppressWarnings("deprecation") // We know GridFS uses the old API. A new API version will be address later. private static GridFS getGridFS() throws Exception { if (gridFS == null) { gridFS = new GridFS(getMongo().getDB(db)); } return gridFS; }
/** * Returns the DB object from a newly created Mongo instance based on this URI. * * @return the database specified in the URI. This will implicitly create a new Mongo instance, which must be closed manually. * @throws MongoException if there's a failure */ public DB connectDB() { return connect().getDB(getDatabaseNonNull()); }
public void shouldCreateNewObjectInEmbeddedMongoDb() { DB db = mongo.getDB(DATABASE_NAME); DBCollection col = db.createCollection("testCollection", new BasicDBObject());
public static void main(String[] args) throws UnknownHostException, MongoException { Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("yeahMongo"); Employee employee = new Employee(); employee.setNo(1L); employee.setName("yogesh"); DBCollection employeeCollection = null ; employeeCollection = db.getCollection(Employee.COLLECTION_NAME); employeeCollection.save(employee); System.err.println(employeeCollection.findOne()); }
Mongo mongo = new Mongo(); DBObject ping = new BasicDBObject("ping", "1"); try { mongo.getDB("dbname").command(ping); } catch (MongoException e) { ... }
Mongo m = new Mongo( "localhost" , 27017 ); DB db = m.getDB( "mydb" );
Mongo m = new Mongo("localhost", 27017); DB db = m.getDB("test"); DBCollection myColl = db.getCollection("myCollection"); BasicDBObject docToInsert = new BasicDBObject("resourceID", "3"); docToInsert.put("resourceName", "Foo Test3"); BasicDBObject updateQuery = new BasicDBObject("_id", "1"); updateQuery.put("itemList.itemID", "1"); BasicDBObject updateCommand = new BasicDBObject("$push", new BasicDBObject("itemList.$.resources", docToInsert)); myColl.update(updateQuery, updateCommand); System.out.println(myColl.findOne().toString());
Mongo mongo = new Mongo("127.0.0.1"); DB db = mongo.getDB("mydbid"); DBCollection collection; if (db.collectionExists("mycollection")) { collection = db.getCollection("mycollection"); } else { DBObject options = BasicDBObjectBuilder.start().add("capped", true).add("size", 2000000000l).get(); collection = db.createCollection("mycollection", options); } }
public Nextstud() { Mongo s = new Mongo(); DB db = s.getDB( "omrs1" ); DBCollection coll = db.getCollection("Student") ;
/** * Drops the database if it exists. * * @param dbName name of database to drop * @throws MongoException if the operation fails */ public void dropDatabase(final String dbName) { getDB(dbName).dropDatabase(); }
/** * Returns the DB object from a newly created Mongo instance based on this URI. * * @return the database specified in the URI. This will implicitly create a new Mongo instance, which must be closed manually. * @throws MongoException if there's a failure */ public DB connectDB() { return connect().getDB(getDatabaseNonNull()); }
public DatastoreImpl(final Mapper mapper, final Mongo mongo, final String dbName) { this.mapper = mapper; this.mongo = mongo; db = mongo.getDB(dbName); // VERY discussable DatastoreHolder.getInstance().set(this); }