MongoClient 操作MongoDB replica-set

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

对于MongoDB的Java驱动, 从2.10.0版本后,文档中提醒Mongo类将会被废除,现在开始都鼓励使用MongoClient类。

下面演示一个Java程序如何使用最新的MongoClient类来对MongoDB写操作。

首先假定已经有了一个Replica-set集群,分别是d1, d2和 d3三台虚拟机。

然后创建一个Maven构建的Java应用程序。使用了maven exec plugin用来方便执行jar包和定制参数。

看一下pom.xml:

  1.  <build>  
  2.    <plugins>  
  3.      <plugin>  
  4. <groupId>org.codehaus.mojo</groupId>  
  5. <artifactId>exec-maven-plugin</artifactId>  
  6. <version>1.2.1</version>  
  7. <executions>  
  8.   <execution>  
  9.     <goals>  
  10.       <goal>java</goal>  
  11.     </goals>  
  12.   </execution>  
  13. </executions>  
  14. <configuration>  
  15.   <mainClass>org.freebird.dbtool.App</mainClass>  
  16.   <arguments>  
  17.     <argument>d1,d2,d3</argument>  
  18.   </arguments>  
  19. </configuration>  
  20.      </plugin>  
  21.    </plugins>  
  22.  </build>  
  <build>    <plugins>      <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <executions>   <execution>     <goals>       <goal>java</goal>     </goals>   </execution> </executions> <configuration>   <mainClass>org.freebird.dbtool.App</mainClass>   <arguments>     <argument>d1,d2,d3</argument>   </arguments> </configuration>      </plugin>    </plugins>  </build>

传递了三个参数,中间用,隔开,分别是不同的主机名称:d1, d2, d3.

看看代码初始化部分:

    public static void main(String[] args) throws UnknownHostException { System.out.println(args[0]);        String[] hosts = args[0].split(",");        int portNumber = 27017;        System.out.println(hosts[0]);        System.out.println(hosts[1]);        System.out.println(hosts[2]);                MongoClient client = new MongoClient(Arrays.asList(new ServerAddress(hosts[0], portNumber),                                      new ServerAddress(hosts[1], portNumber),                                      new ServerAddress(hosts[2], portNumber)));        MyApp.getInstance().setDbName("kaimei");        MyApp.getInstance().setClient(client);

这里将三个host用,分割开后,创建三个ServerAddress对象,然后构建MongoClient对象。

同时创建了一个MyApp的singleton对象,存放这个MongoClient对象,并提供了getDB()方便日后获取数据库连接。

public class MyApp {        private MyApp() {    }        public static MyApp getInstance() {        return MyAppHolder.INSTANCE;    }        private static class MyAppHolder {        private static final MyApp INSTANCE = new MyApp();    }        @Getter @Setter    String dbName;        @Setter    MongoClient client;        public DB getDB() {        return client.getDB(dbName);    }}

以后在任何地方需要使用连接的时候,这样使用:

    public static void bind(final String address, final String userId) { DB db = MyApp.getInstance().getDB(); DBCollection collection = db.getCollection(DISPLAY_COLLECTION); DBObject condition = new BasicDBObject(); condition.put("address", address); DBObject field = new BasicDBObject(); field.put("user_id", new ObjectId(userId)); DBObject set = new BasicDBObject(); set.put("$set", field); collection.update(condition, set, false, false);    }

很简单吧。

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值