本文整理了Java中com.mongodb.Mongo
类的一些代码示例,展示了Mongo
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Mongo
类的具体详情如下:
包路径:com.mongodb.Mongo
类名称:Mongo
[英]A database connection with internal connection pooling. For most applications, you should have one Mongo instance for the entire JVM.
Note: This class has been superseded by MongoClient, and may be deprecated in a future release.
[中]具有内部连接池的数据库连接。对于大多数应用程序,整个JVM都应该有一个Mongo实例。
注意:此类已被MongoClient取代,在未来的版本中可能会被弃用。
代码示例来源:origin: org.mongodb/mongo-java-driver
/**
* 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);
}
代码示例来源:origin: stackoverflow.com
mongodExe = runtime.prepare(new MongodConfig(Version.V2_3_0, 12345, Network.localhostIsIPv6()));
mongod = mongodExe.start();
mongo = new Mongo("localhost", 12345);
public void shouldCreateNewObjectInEmbeddedMongoDb() {
DB db = mongo.getDB(DATABASE_NAME);
DBCollection col = db.createCollection("testCollection", new BasicDBObject());
col.save(new BasicDBObject("testDoc", new Date()));
assertThat(col.getCount(), Matchers.is(1L));
代码示例来源:origin: org.mongodb/mongo-java-driver
/**
* Closes all resources associated with this instance, in particular any open network connections. Once called, this instance and any
* databases obtained from it can no longer be used.
*/
public void close() {
super.close();
}
代码示例来源:origin: spring-projects/spring-data-book
@Override
public Mongo mongo() throws Exception {
Mongo mongo = new Mongo();
mongo.setWriteConcern(WriteConcern.SAFE);
return mongo;
}
代码示例来源:origin: stackoverflow.com
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());
}
代码示例来源:origin: stackoverflow.com
private DBCursor curs;
public Nextstud() {
Mongo s = new Mongo();
DB db = s.getDB( "omrs1" );
DBCollection coll = db.getCollection("Student") ;
curs = coll.find();
代码示例来源:origin: stackoverflow.com
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());
代码示例来源:origin: stackoverflow.com
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);
代码示例来源:origin: stackoverflow.com
// connect to MongoDB server.
Mongo mongo = new Mongo("localhost", 27017);
DB database = mongo.getDB("mydb");
DBCollection collection = database.getCollection("testCollection");
// create a simple db object where counter value is 0
DBObject temp = new BasicDBObject("name", "someName").append("counter", 0);
// insert it into the collection
collection.insert(temp);
// create an increment query
DBObject modifier = new BasicDBObject("counter", 1);
DBObject incQuery = new BasicDBObject("$inc", modifier);
// create a search query
DBObject searchQuery = new BasicDBObject("name", "someName");
// increment a counter value atomically
WriteResult upRes = collection.update(searchQuery, incQuery);
代码示例来源:origin: stackoverflow.com
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("dbName");
DBCollection collection = db.getCollection("collectionName");
List distinctCity = collection.distinct("cityname");
for(int i = 0; i < distinctCity.size(); i++) {
BasicDBObject query = new BasicDBObject();
query.put("cityname", distinctCity.get(i));
BasicDBObject project = new BasicDBObject();
project.put("JAN", 1);
project.put("_id", 0);
DBCursor cursorDoc = collection.find(query, project);
while(cursorDoc.hasNext()) {
BasicDBObject object = (BasicDBObject) cursorDoc.next();
Integer currentValue = object.getInt("JAN");
DBCursor allData = collection.find(new BasicDBObject(), project);
while(allData.hasNext()) {
BasicDBObject allDataObject = (BasicDBObject) allData.next();
Integer allDataJanValue = allDataObject.getInt("JAN");
Integer result = currentValue - allDataJanValue;
System.out.print(" " + result + " ");
}
System.out.println();
}
}
}
代码示例来源:origin: eBay/YiDB
public void loadProperties(InputStream is) {
DBCollection propertiesCollection = mongo.getDB(CMSConsts.SYS_DB).getCollection(CMSConsts.PROPERTIES_COLLECTION);
//load properties collection
BasicDBObject cmsProperties = (BasicDBObject)loadBasonFromFile(is);
for (String key : cmsProperties.keySet()) {
DBObject obj = new BasicDBObject().append(key, cmsProperties.get(key));
propertiesCollection.insert(obj);
}
}
代码示例来源:origin: eBay/YiDB
@Test
public void testWrapperUpdate() {
MongoDataSource ds = getDataSource();
Mongo mongo = ds.getMongoInstance();
mongo.dropDatabase(DB);
DBCollection coll = mongo.getDB(DB).getCollection(COLL);
coll.setWriteConcern(WriteConcern.SAFE);
BasicDBObject o1 = new BasicDBObject().append("name", "value");
coll.insert(o1);
BasicDBObject o2 = new BasicDBObject().append("name", "newValue");
BasicDBObject update = new BasicDBObject().append("$set",
o2);
Assert.assertTrue(MongoUtils.wrapperUpdate(coll, o1, update));
Assert.assertEquals(1, coll.count(o2));
Assert.assertFalse(MongoUtils.wrapperUpdate(coll, o1, update));
}
}
代码示例来源:origin: stackoverflow.com
Mongo m = new Mongo();
m.setWriteConcern(WriteConcern.SAFE);
DBCollection c = m.getDB("test").getCollection("or-test");
c.drop();
c.insert(new BasicDBObject("a", "abba"));
c.insert(new BasicDBObject("b", "abba"));
c.insert(new BasicDBObject("a", "bbba"));
c.insert(new BasicDBObject("b", "bbba"));
Pattern pattern = Pattern.compile("^a");
DBObject query = QueryBuilder.start().or(
QueryBuilder.start("a").regex(pattern).get(),
QueryBuilder.start("b").regex(pattern).get()
).get();
System.out.println(c.find(query).count());
代码示例来源:origin: stackoverflow.com
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));
代码示例来源:origin: io.rhiot/rhiot-cloudplatform-service-device
@Override
public void write(String deviceId, String metric, Object value) {
BasicDBObject metricRecord = new BasicDBObject(of(
FIELD_DEVICE_ID, deviceId, FIELD_METRIC, metric, "value", value, "timestamp", new Date()
));
mongo.getDB(db).getCollection(collection).save(metricRecord);
}
代码示例来源:origin: org.mongodb/mongo-hadoop-core
/**
* Contacts the config server and builds a map of each shard's name to its host(s) by examining config.shards.
*/
protected Map<String, String> getShardsMap() {
DB configDB = this.mongo.getDB("config");
final HashMap<String, String> shardsMap = new HashMap<String, String>();
DBCollection shardsCollection = configDB.getCollection("shards");
DBCursor cur = shardsCollection.find();
try {
while (cur.hasNext()) {
final BasicDBObject row = (BasicDBObject) cur.next();
String host = row.getString("host");
// for replica sets host will look like: "setname/localhost:20003,localhost:20004"
int slashIndex = host.indexOf('/');
if (slashIndex > 0) {
host = host.substring(slashIndex + 1);
}
shardsMap.put((String) row.get("_id"), host);
}
} finally {
cur.close();
}
return shardsMap;
}
代码示例来源:origin: Stratio/wikipedia-parser
protected MongoBackend() throws MongoException, UnknownHostException {
m = new Mongo();
db = m.getDB("wpv");
articlesCollection = db.getCollection("articles");
}
代码示例来源:origin: stackoverflow.com
Mongo mongo = ...
DB db = mongo.getDB("yourDbName");
DBCollection coll = db.getCollection("person");
DBObject query = new BasicDBObject();
query.put("source", "Naukri");
query.put("dateCreated", new BasicDBObject($exists : true));
DBCursor cur = coll.find(query).sort(new BasicDBObject("dateCreated", 1)).limit(10);
while(cur.hasNext()) {
DBObject obj = cur.next();
// Get data from the result object here
}
代码示例来源:origin: eBay/YiDB
@Before
public void before() {
mongo = getDataSource().getMongoInstance();
dbName = "MongoMutexTest";
collName = "lockColl";
mongo.getDB(dbName).getCollection(collName).drop();
}
代码示例来源:origin: eBay/YiDB
@Test
public void testLoadProperties() {
DBCollection coll = mongo.getDB(CMSConsts.SYS_DB).getCollection(CMSConsts.PROPERTIES_COLLECTION);
DBCursor cursor = coll.find();
BasicDBObject object = (BasicDBObject)cursor.next();
Assert.assertEquals(5000, object.getInt("RepositoryCacheSize"));
object = (BasicDBObject)cursor.next();
Assert.assertEquals(3600, object.getInt("RepositoryCacheExpireSeconds"));
}
内容来源于网络,如有侵权,请联系作者删除!