mongodb java接入
mongodb的java驱动已经提供比较全的操作,分为三块:
1.普通用户操作
2.管理员操作
3.oplog操作
具体可以添加一个eclipse testng插件,再在工程中导入testng.jar包即可看到。
本文主要说明各个使用点和注意事项。
1.连接一个复制集(replica sets)mongod:
复制集是mongo解决稳定性的一个方案,也是可以实现读写分离的一种实现,这里介绍复制集的java连接方式。
static Mongo m;
static {
List<ServerAddress> addresslist = new ArrayList<ServerAddress>();
try {
addresslist.add(new ServerAddress("127.0.0.1:10240"));
addresslist.add(new ServerAddress("127.0.0.1:11240"));
addresslist.add(new ServerAddress("127.0.0.1:12240"));
} catch (UnknownHostException e) {
System.err.println("address check error.");
System.exit(-1);
}
MongoOptions options = new MongoOptions();
options.autoConnectRetry = true;
options.connectionsPerHost = 20;
options.connectTimeout = 6000;
options.maxAutoConnectRetryTime = 12000;
options.maxWaitTime = 12000;
options.socketKeepAlive = true;
options.socketTimeout = 2000;
try {
m = new Mongo(addresslist, options);
} catch (MongoException e) {
System.err.println("mongo create error.");
System.exit(-1);
}
}
static Mongo m;
static {
List<ServerAddress> addresslist = new ArrayList<ServerAddress>();
try {
addresslist.add(new ServerAddress("127.0.0.1:10240"));
addresslist.add(new ServerAddress("127.0.0.1:11240"));
addresslist.add(new ServerAddress("127.0.0.1:12240"));
} catch (UnknownHostException e) {
System.err.println("address check error.");
System.exit(-1);
}
MongoOptions options = new MongoOptions();
options.autoConnectRetry = true;
options.connectionsPerHost = 20;
options.connectTimeout = 6000;
options.maxAutoConnectRetryTime = 12000;
options.maxWaitTime = 12000;
options.socketKeepAlive = true;
options.socketTimeout = 2000;
try {
m = new Mongo(addresslist, options);
} catch (MongoException e) {
System.err.println("mongo create error.");
System.exit(-1);
}
}注:一般例子采用的连接方式是直接连一个mongod服务,但是作为单台的生产环境显然安全性不足,因此采用复制集方式,连接也区别于一般,是使用
public Mongo( List<ServerAddress> replicaSetSeeds , MongoOptions options ) throws MongoException
public Mongo( List<ServerAddress> replicaSetSeeds , MongoOptions options ) throws MongoException方法。
MongoOptions根据自己的需求设置,参数说明已经很多了,不细讲。
注意mongo实例使用单例模式。
2.获取db:
DB db = m.getDB("test");
DB db = m.getDB("test");注:取到名为test的db进行操作;若该db不存在,该操作会创建对应的db。
3.获取集合:
DBCollection col = db.getCollection("offer");
DBCollection col = db.getCollection("offer");
注:取名字为offer的集合;类似于db的表。
以上三个操作是建立连接,接下去才是db中常用的操作。
4.查询:
DBCursor cursor = col.find();
while(cursor.hasNext()) {
System.out.println(cursor.next());
i++;
}
DBCursor cursor = col.find();
while(cursor.hasNext()) {
System.out.println(cursor.next());
i++;
}注:全集合查询,输出每条记录,正常情况不会作类似操作,记住两个东西,find()和cursor
BasicDBObject query = new BasicDBObject();
query = new BasicDBObject();
query.put("i", new BasicDBObject("$gt", 20).append("$lte", 30)); // i.e. 20 < i <= 30
<pre name="code" class="java"> DBCursor cur = coll.find(query);
while(cur.hasNext()) {
System.out.println(cur.next());
}
BasicDBObject query = new BasicDBObject();
query = new BasicDBObject();
query.put("i", new BasicDBObject("$gt", 20).append("$lte", 30)); // i.e. 20 < i <= 30
<pre name=&qu