package com.wdh.mongo;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
/**
* mongodb client for java
* @version 2.13
* */
public class MongoTest {
private String host= "192.168.78.132";
private int port = 27017;
private String userName = "wdh";
private char[] password = "wdh".toCharArray();
public void connTest()throws Exception{
ServerAddress address = new ServerAddress(host,port);
List<ServerAddress> addresses = new ArrayList<ServerAddress>();
addresses.add(address);
MongoCredential credential = MongoCredential.createCredential(userName, "wdh", password);
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
MongoClient client = new MongoClient(addresses,credentials);
DB db = client.getDB("wdh");
DBCollection collection = db.getCollection("person");
}
public void findTest()throws Exception{
ServerAddress address = new ServerAddress(host,port);
List<ServerAddress> addresses = new ArrayList<ServerAddress>();
addresses.add(address);
MongoCredential credential = MongoCredential.createCredential(userName, "wdh", password);
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
MongoClient client = new MongoClient(addresses,credentials);
DB db = client.getDB("wdh");
DBCollection collection = db.getCollection("person");
DBObject query = new BasicDBObject();
query.put("name", "zhangsan");
DBCursor cursor = collection.find(query);
//collection.find(query).sort(DBObject).limit(100).skip(100);
DBObject item = null;
while(cursor.hasNext()){
item =cursor.next();
System.out.println(item.get("age"));
}
}
public void insertTest()throws Exception{
ServerAddress address = new ServerAddress(host,port);
List<ServerAddress> addresses = new ArrayList<ServerAddress>();
addresses.add(address);
MongoCredential credential = MongoCredential.createCredential(userName, "wdh", password);
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
MongoClient client = new MongoClient(addresses,credentials);
DB db = client.getDB("wdh");
DBCollection collection = db.getCollection("person");
BasicDBObject query = new BasicDBObject();
query.append("name", "wangdonghua");
query.append("age", 31);
query.append("salary", 8888);
query.append("dept", "dev");
collection.insert(query);
}
public void removeTest()throws Exception{
ServerAddress address = new ServerAddress(host,port);
List<ServerAddress> addresses = new ArrayList<ServerAddress>();
addresses.add(address);
MongoCredential credential = MongoCredential.createCredential(userName, "wdh", password);
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
MongoClient client = new MongoClient(addresses,credentials);
DB db = client.getDB("wdh");
DBCollection collection = db.getCollection("person");
DBObject query = new BasicDBObject();
query.put("name", "wangdonghua");
collection.remove(query);
}
public void updateTest()throws Exception{
ServerAddress address = new ServerAddress(host,port);
List<ServerAddress> addresses = new ArrayList<ServerAddress>();
addresses.add(address);
MongoCredential credential = MongoCredential.createCredential(userName, "wdh", password);
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
MongoClient client = new MongoClient(addresses,credentials);
DB db = client.getDB("wdh");
DBCollection collection = db.getCollection("person");
BasicDBObject query = new BasicDBObject();
query.put("name", "wangdonghua");
DBCursor cursor = collection.find(query);
BasicDBObject item = null;
while(cursor.hasNext()){
item = (BasicDBObject)cursor.next();
item.append("age", 18);
collection.save(item);
}
}
}
----------------------------------相关参考-----------------------------------------------
http://docs.mongodb.org/ecosystem/tutorial/use-aggregation-framework-with-java-driver/
相关推荐
mongodb java Driver,具体使用请看我的博客:http://blog.csdn.net/lablenet
mongodb-orm简介Mongodb ORM是基于java的ORM框架,简化了SDK的使用,使代码变得更清晰、简单。 与Ibatis类似,将查询、执行语句封装在xml中,与代码隔离。简称MQL。 项目中使用加入mongodb orm的支持包1. 添加jar...
本示例将地图缓存入库到Mongodb数据里,便于其他客户端使用地图缓存,只需拷贝缓存的sci文件即可
MongOz 是一个 MongoDB 客户端,旨在成为多平台。 它是用 Java 编写的作为后端(处理 MongoDB 连接)和 Javascript / HTML5 / CSS3 作为前端,除了易于根据需要进行更改之外,还具有现代界面。
这个客户端实现了对mongoDB数据库的并发访问及访问性能测试。包括查询,修改和插入性能。用户使用该客户端的时候需要修改mongodb的ip地址和端口号,以及数据库名称和collection名称。
import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoDatabase; import ...
项目介绍gameserver项目是一个java游戏框架,包括测试用client,netty客户端.基于Spring+netty+redis+自定义rpc(或者dubbo)+MyBatis + mysql(或者mongodb)实现。socket通信实现,登录系统,玩家线程模型,db系统,分布式...
What You'll Learn:* How to use a Java client and MongoDB shell * How to use MongoDB with PHP, Ruby, and Node.js as well* How to migrate Apache Cassandra tables to MongoDB documents; Couchbase to ...
1、开发server和client端 定时向zk集群发送心跳数据包,利用Java自带的timer程序实现该功能 2、开发整天的monitor程序,用来定时向zk中获取server和client端的心跳数据信息,如果超过指定时间没有收到最新的数据包,...
MongoDB Java服务器伪造的Java核心服务器的实现,可用于集成测试。 考虑一下H2 / HSQLDB / SQLite,但要使用MongoDB。 是通过实现的。 不同的后端是可能的,并且可以扩展。内存后端内存后端是默认后端,通常用于伪造...
研究各种Java技术(Springboot+Redis+Dubbo+Rocketmq),含如下模块: whatsmars-common mongodb,mail,AES,DES加解密 whatsmars-earth-web springmvc+...whatsmars-motan-demo weibo分布式RPC框架 demo server&client;
今天在配置MongoDB时发生了以下几个错误, 已经被我解决了,提供给大家. 2015-05-12T09:30:26.313+0800 I STORAGE [initandlisten] exception in initAndListen: 28574 Cannot start server. Detected data files in /...
java-nats.zip,支持在spring.nats client for java中使用nats client
Mongodb提供了多种开发语言的驱动,java,python,c++,c# 等,这里选用c#驱动作为测试; 首先上mongo官网下载驱动。Ps:官方网站经常连接不顺利。 还不如直接在vs的nuget管理包中搜索mongoDB.driver. 需要引入的命名...
Accessing server-side mongodb through client javascript API. This project is a branch of [dbcloud](https://github.com/csmake/dbcloud) 你几乎可以完成绝大部分MongoDB调用通过浏览器的JavaScript API。 You...
canal-client 项目介绍 canal客户端、解析binlog日志将数据从mysql过渡到rabbitmq和mongodb中 软件架构 软件架构说明 安装教程 下载最新版本的canal服务端, 解压缩 mkdir /tmp/canal tar zxvf canal.deployer-$...
研究各种Java技术(Springboot+Redis+Dubbo+Rocketmq),地址https://github.com/javahongxi/whatsmars,为maven项目,含如下模块: ...whatsmars-motan-demo weibo分布式RPC框架 demo server&client
chat.thrift运行jar文件在根项目目录上运行gradle clientJar serverJar 确保MongoDB正在运行在build/libs文件夹中,运行java -jar chat-server-1.0.jar和java -jar chat-client-1.0.jar测试脚本确保MongoDB正在运行...
跑腿源码java 节点新闻-nodejs-angular 从 api 获取消息并将其保存到 mongodb 并在前端开发人员中显示 cd 进入server cd ./server并在运行命令后执行npm i : npm run start to exec Api。 使用另一个终端 cd 进入...