本文整理汇总了Java中com.mongodb.Mongo类的典型用法代码示例。如果您正苦于以下问题:Java Mongo类的具体用法?Java Mongo怎么用?Java Mongo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Mongo类属于com.mongodb包,在下文中一共展示了Mongo类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getEstimatedSizeBytes
import com.mongodb.Mongo; //导入依赖的package包/类
@Override
public long getEstimatedSizeBytes(PipelineOptions options) throws Exception {
Mongo mongo = spec.connectionConfiguration().setupMongo();
try {
GridFS gridfs = spec.connectionConfiguration().setupGridFS(mongo);
DBCursor cursor = createCursor(gridfs);
long size = 0;
while (cursor.hasNext()) {
GridFSDBFile file = (GridFSDBFile) cursor.next();
size += file.getLength();
}
return size;
} finally {
mongo.close();
}
}
示例2: NaviMongoListDriver
import com.mongodb.Mongo; //导入依赖的package包/类
public NaviMongoListDriver(ServerUrlUtil.ServerUrl server, String auth, NaviPoolConfig poolConfig) throws NumberFormatException, MongoException, UnknownHostException {
super(server, auth, poolConfig);
String masterUrl = null;
if (server.getHost() != null && server.getPort() != 0)
masterUrl = server.getHost() + ":" + server.getPort();
List<ServerAddress> addresslist = new ArrayList<>();
// 找到master
List<String> listHostPorts = new ArrayList<>();
String[] hostPorts = server.getUrl().split(",");
Collections.addAll(listHostPorts, hostPorts);
for (int i = 0; i < listHostPorts.size(); i++) {
if (listHostPorts.get(0).equals(masterUrl))
break;
listHostPorts.add(listHostPorts.remove(0));
}
for (String hostPort : listHostPorts) {
addresslist.add(new ServerAddress(hostPort));
}
mongo = new Mongo(addresslist, getMongoOptions(poolConfig));
// mongo.setReadPreference(ReadPreference.SECONDARY);
startIdleConnCheck();
}
示例3: getMongo
import com.mongodb.Mongo; //导入依赖的package包/类
public Mongo getMongo() {
if (isClose()) {
throw new NaviSystemException("the driver has been closed!",
NaviError.SYSERROR);
}
/*
try {
log.info("get mongo is open:" + mongo.getConnector().isOpen());
// int count = mongo.getConnector().getDBPortPool(new
// ServerAddress(server.getHost(), server.getPort())).available();
// log.info("get mongo available:" + count);
log.info("getConnectPoint:"
+ mongo.getConnector().getConnectPoint());
} catch (Exception e) {
// TODO Auto-generated catch block
// UnknownHostException
e.printStackTrace();
}
*/
return mongo;
}
示例4: mongo
import com.mongodb.Mongo; //导入依赖的package包/类
@Override
public Mongo mongo() throws Exception {
final MongoCredential credential = MongoCredential.createCredential(
mongoClientUri().getUsername(),
getDatabaseName(),
mongoClientUri().getPassword());
final String hostUri = mongoClientUri().getHosts().get(0);
final String[] host = hostUri.split(":");
return new MongoClient(new ServerAddress(
host[0], host.length > 1 ? Integer.parseInt(host[1]) : DEFAULT_PORT),
Collections.singletonList(credential),
mongoClientOptions());
}
示例5: newMongoDbClient
import com.mongodb.Mongo; //导入依赖的package包/类
/**
* New mongo db client.
*
* @param mongo the mongo
* @return the mongo
*/
public static Mongo newMongoDbClient(final AbstractMongoInstanceProperties mongo) {
return new MongoClient(new ServerAddress(
mongo.getHost(),
mongo.getPort()),
Collections.singletonList(
MongoCredential.createCredential(
mongo.getUserId(),
mongo.getDatabaseName(),
mongo.getPassword().toCharArray())),
newMongoDbClientOptions(mongo));
}
示例6: mongo
import com.mongodb.Mongo; //导入依赖的package包/类
@Override
@Bean(destroyMethod = "close")
public Mongo mongo() throws Exception {
return new EmbeddedMongoBuilder()
.version("3.4.2")
.build();
}
示例7: mongo
import com.mongodb.Mongo; //导入依赖的package包/类
/**
* @return A new in memory MongoDB.
*/
@Bean(destroyMethod = "close")
public static Mongo mongo() throws IOException {
return new EmbeddedMongoBuilder()
.version("2.4.5")
.bindIp("127.0.0.1")
.port(12345)
.build();
}
示例8: getMongo
import com.mongodb.Mongo; //导入依赖的package包/类
private Mongo getMongo(MongoClientURI mongoURI) {
try {
Mongo mongo = new MongoClient(mongoURI);
activityMongos.add(mongo);
return mongo;
} catch (UnknownHostException e) {
throw new CannotGetMongoDbConnectionException(String.format("con't get mongo '%s' connection!", mongoURI.getHosts()));
}
}
示例9: destroy
import com.mongodb.Mongo; //导入依赖的package包/类
@Override
public void destroy() throws Exception {
if (!CollectionUtils.isEmpty(activityMongos)) {
for (Mongo mongo : activityMongos) {
mongo.close();
}
}
}
示例10: mongo
import com.mongodb.Mongo; //导入依赖的package包/类
@SuppressWarnings("deprecation")
@Override
public Mongo mongo() {
try {
return new Mongo("mongodb://localhost/cityoffice");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
示例11: mongo
import com.mongodb.Mongo; //导入依赖的package包/类
@Override
public Mongo mongo() throws Exception {
/**
*
* this is for a single db
*/
return new MongoClient();
}
示例12: mongo
import com.mongodb.Mongo; //导入依赖的package包/类
@Override
public
@Bean
Mongo mongo() {
// uses fongo for in-memory tests
return new Fongo("integration-test").getMongo();
}
示例13: mongo
import com.mongodb.Mongo; //导入依赖的package包/类
@Override
@Bean
public Mongo mongo() throws Exception {
if (username != null && password != null && username.length() > 0) {
ServerAddress address = new ServerAddress(mongoHost, mongoPort);
MongoCredential credential = MongoCredential.createCredential(username, mongoDatabasename, password.toCharArray());
List<MongoCredential> credentials = new ArrayList<>();
credentials.add(credential);
return new MongoClient(address, credentials);
}
return new MongoClient(mongoHost, mongoPort);
}
示例14: mongo
import com.mongodb.Mongo; //导入依赖的package包/类
@Override
public Mongo mongo() throws Exception {
if(!authenticate) {
//Allows testing with a database without users and authentication configured.
//should not be used in production.
log.warn("MongoDB is accessed without any authentication. " +
"This is strongly discouraged and should only be used in a testing environment.");
return new MongoClient(hostname);
} else {
List<MongoCredential> cred = new LinkedList<>();
cred.add(MongoCredential.createCredential(dbUserName, dbAuthDB, dbPasswd.toCharArray()));
ServerAddress addr = new ServerAddress(hostname, dbPort);
return new MongoClient(addr, cred);
}
}
示例15: mongo
import com.mongodb.Mongo; //导入依赖的package包/类
@Override
public Mongo mongo() throws Exception {
if (!StringUtils.isEmpty(getUsername()) && !StringUtils.isEmpty(getPassword())) {
try {
MongoCredential credential = MongoCredential.createCredential(getUsername(), getDatabaseName(), getPassword().toCharArray());
return new MongoClient(hostname, Collections.singletonList(credential));
} catch (Exception e) {
return new MongoClient(hostname);
}
} else {
return new MongoClient(hostname);
}
}