/** * Creates a Mongo instance based on the URI. * * @return a new Mongo instance. There is no caching, so each call will create a new instance, each of which must be closed manually. * @throws MongoException if there's a failure */ @SuppressWarnings("deprecation") public Mongo connect() { // Note: we can't change this to new MongoClient(this) as that would silently change the default write concern. return new Mongo(this); }
@Override public Mongo mongo() throws Exception { Mongo mongo = new Mongo(); mongo.setWriteConcern(WriteConcern.SAFE); return mongo; }
mongodExe = runtime.prepare(new MongodConfig(Version.V2_3_0, 12345, Network.localhostIsIPv6())); mongod = mongodExe.start(); mongo = new Mongo("localhost", 12345);
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 mongo = new Mongo(); try { mongo.getConnector().getDBPortPool(mongo.getAddress()).get().ensureOpen(); } catch (Exception e) { ... }
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); } }
mongoTemplate = new MongoTemplate(new Mongo(host, port), db, new UserCredentials(username, password));
public class Foo extends Bar { private Mongo mongo; public Foo() throws SomeException { mongo = new Mongo(); } // stuff goes here }
/** * Creates a Mongo instance based on the URI. * * @return a new Mongo instance. There is no caching, so each call will create a new instance, each of which must be closed manually. * @throws MongoException if there's a failure */ @SuppressWarnings("deprecation") public Mongo connect() { // Note: we can't change this to new MongoClient(this) as that would silently change the default write concern. return new Mongo(this); }
private DBCursor curs; public Nextstud() { Mongo s = new Mongo(); DB db = s.getDB( "omrs1" ); DBCollection coll = db.getCollection("Student") ;
@Service public class MongoService { private final Mongo mongo; private final String database; private static final Logger LOGGER = LoggerFactory.getLogger(MongoService.class); @bean public MongoService(@Nonnull final @Value("#{ systemProperties['host']}") String host, @Nonnull final @Value("#{ systemProperties['port']}") int port, @Nonnull final @Value("#{ systemProperties['database']}") String db) throws UnknownHostException { LOGGER.info("host=" + host + ", port=" + port + ", database=" + db); mongo = new Mongo(host, port); database = db; } }
/** * Constructs a new {@code MongoConnection}. * * @param host The host address. * @param port The port. * @param database The database name. * @throws Exception If an error occurred while trying to connect. */ public MongoConnection(String host, int port, String database) throws Exception { mongo = new Mongo(host, port); db = mongo.getDB(database); }
Mongo m = new Mongo("localhost", 27017); DB db = m.getDB("test"); DBCollection myColl = db.getCollection("collection"); ArrayList<String> newResourceIDList = new ArrayList<String>(); newResourceIDList.add("Something"); newResourceIDList.add("new"); BasicDBObject myQuery = new BasicDBObject("_id", "1"); myQuery.put("projectList.projectID", "Spring"); BasicDBObject myUpdate = new BasicDBObject("$set", new BasicDBObject("projectList.$.resourceIDList", newResourceIDList)); myColl.update(myQuery, myUpdate); System.out.println(myColl.findOne().toString());
public DB connect() throws IOException { ServerAddress server = new ServerAddress(host, port); Mongo mongo = new Mongo(server); DB database = mongo.getDB(db); database.authenticate(user, passwd != null ? passwd.get(): new char[]{}); return database; } }
public Mongo newMongo() throws UnknownHostException, MongoException { return new Mongo(new ServerAddress( mongodProcess.getConfig().net().getServerAddress(), mongodProcess.getConfig().net().getPort())); }
Mongo mongo = new Mongo(); DB db = mongo.getDB("test"); DBCollection people = db.getCollection("people"); BasicDBObject name = new BasicDBObject(); name.put("FirstName", "Ahmad"); name.put("LastName", "Khan"); BasicDBObject person = new BasicDBObject(); person.put("ID", 23); person.put("Name", name); people.insert(person);
public MongoMongo(CSDNMongoConfiguration csdnMongoConfiguration) throws Exception { this.mongoConfiguration = csdnMongoConfiguration; this.mongo = new Mongo(settings().get(mode() + ".datasources.mongodb.host", defaultHostName), mongoConfiguration.settings.getAsInt(mode() + ".datasources.mongodb.port", defaultHostPort)); dbName = settings().get(mode() + ".datasources.mongodb.database", defaultDBName); }