1.先连接你的mongodb

看连接是否有问题,代码

public class MongoDB2 {

	private static MongoDatabase mongoDatabase = null;
private static int port = 27017;
private static String userName = "XX";
private static String password="XX" ;
private static String database = "gatp";
private static String host="XXX"; /**
* mongo db 连接
*
*
*/
public void mongoConnect() {
try {
// host和port进行转换
encryptionDecryption decryption = new encryptionDecryption();
ServerAddress serverAddress = new ServerAddress(host, port);
List<ServerAddress> addresses = new ArrayList<ServerAddress>();
addresses.add(serverAddress);
MongoCredential credential = MongoCredential.createCredential(userName,database, password.toCharArray());
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
// 通过连接认证获取MongoDB连接
MongoClient mongoClient = new MongoClient(addresses, credentials);
mongoDatabase = mongoClient.getDatabase(database);
Log.logInfo(mongoDatabase);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
} public static void main(String[] args) {
MongoDB2 db=new MongoDB2();
db.mongoConnect(); //确认连接正确
}

  连接成功后会显示mogodb的id,错误会显示认证失败

连接失败的案例

成功会显示

2.对mogodb进行数据的插入

封装的方法insertCollection,插入可数字,字符串,

	public boolean insertCollection(String collectionName, List<Document> documents) {
boolean insertResult = false; try {
MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
collection.insertMany(documents);
insertResult = true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return insertResult;
} public static void main(String[] args) {
MongoDB2 db=new MongoDB2();
db.mongoConnect(); //确认连接正确
Date day=new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//生成json字符串
JSONObject json = new JSONObject();
json.put("id","1");
json.put("name","张三");
json.put("pwd","123456");
System.out.println(json); Document testDocument = new Document();
testDocument.put("times", df.format(day));//插入时间
testDocument.put("name","zhangjun" ); //插入名称
testDocument.put("info", json.toString()); //插入json字符串 List<Document> documents = new ArrayList<Document>(); documents.add(testDocument);
db.insertCollection("test_log_info", documents);
}

  

3.查询数据

/**
* 获取集合
*
* @param collectionName
* 集合名
* @param testDocument
* 条件 , 支持多对条件
* @return
*
*/
public MongoCursor<Document> getCollection(String collectionName, Document testDocument) { MongoCursor<Document> mongoCursor = null;
try {
MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
FindIterable<Document> resultDocument = collection.find(testDocument);
mongoCursor = resultDocument.iterator();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return mongoCursor;
} public static void main(String[] args) {
MongoDB2 db=new MongoDB2();
db.mongoConnect(); //确认连接正确 Document testDocument = new Document();
testDocument.put("name", "zhangjun");
MongoCursor<Document> resultDocument = db.getCollection("test_log_info", testDocument);
while(resultDocument.hasNext()){
System.out.println(resultDocument.next());//获取所有
System.out.println(resultDocument.next().get("_id")); //获取某个值
}
}

java 使用mongodb的更多相关文章

  1. 【MongoDB for Java】Java操作MongoDB

    上一篇文章: http://www.cnblogs.com/hoojo/archive/2011/06/01/2066426.html介绍到了在MongoDB的控制台完成MongoDB的数据操作,通过 ...

  2. JAVA操作MongoDB数据库

    1. 首先,下载MongoDB对Java支持的驱动包 驱动包下载地址:https://github.com/mongodb/mongo-java-driver/downloads 2.Java操作Mo ...

  3. [转]MongoDB for Java】Java操作MongoDB

    原文地址: MongoDB for Java]Java操作MongoDB 开发环境: System:Windows IDE:eclipse.MyEclipse 8 Database:mongoDB 开 ...

  4. Java 连接MongoDB

    1.驱动 通过java连接MongoDB需要一个java版的驱动 下载地址:http://mongodb.github.io/mongo-java-driver/ 2.连接MongoDB 通过 com ...

  5. Java操作MongoDB

    上一篇文章: http://www.cnblogs.com/hoojo/archive/2011/06/01/2066426.html 介绍到了在MongoDB的控制台完成MongoDB的数据操作,通 ...

  6. mongDB基本命令和Java操作MongoDB

    上一篇博文<mongoDB安装>我们安装了mongoDB,现在来复习一下它的一些基本命令:mongoDB的bin目录加入到path之后,命令行中输入mongo: 然后我们进入正题 1.查看 ...

  7. Mongodb快速入门之使用Java操作Mongodb

    [IT168 专稿]在上一篇文章中,我们学习了Mongodb的安装和初步使用,在本文中,将学习如何使用Java去编程实现对Mongodb的操作. HelloWorld程序 学习任何程序的第一步,都是编 ...

  8. Mongodb入门并使用java操作Mongodb

    转载请注意出处:http://blog.csdn.net/zcm101 最近在学习NoSql,先从Mongodb入手,把最近学习的总结下. Mongodb下载安装 Mongodb的下载安装就不详细说了 ...

  9. 浅谈如何用Java操作MongoDB

    NoSQL数据库因其可扩展性使其变得越来越流行,利用NoSQL数据库可以给你带来更多的好处,MongoDB是一个用C++编写的可度可扩展性的开源NoSQL数据库.本文主要讲述如何使用Java操作Mon ...

  10. Java实现mongodb原生增删改查语句

    Java实现mongodb原生增删改查语句 2018-03-16 自动化测试时,需校验数据库数据,为了快速自动化,在代码中用原生增删改查语句操作mongodb 结构 代码 0 pom.xml < ...

随机推荐

  1. Linux字符设备驱动--Led设备驱动

    ①驱动源码 #include <linux/module.h> #include <linux/init.h> #include <linux/cdev.h> #i ...

  2. 两个数据库通过DataTable实现差异传输

    两个主要方法 /// <summary>/// 用途:/// 用源表和目标表比较,返回差异的数据(目标表为参照物)/// /// 逻辑:/// 1.合并两个表/// 2.循环合并后得到的表 ...

  3. shell 0学习

    例一:列出/home 文件夹下的所有文件和目录的详细资料,可以使用如下命令:  ls -a -l /home   ls -al /home 例二:列出当前目录中所有以”d”开头的文件目录的详细内容,可 ...

  4. 安装Office 2013 时提示找不到 Office.zh-cn\OfficeLR.cab

    今天安装office2013的时候总是过会就提示找不到OfficeLR.cab文件 在网上找了好多方法不行,后来将注册表里的office选项全部删除就可以了(HKEY_CURRENT_USER\Sof ...

  5. 性能测试工具LoadRunner03-LR之Virtual User Generator 脚本创建以及回放设置

    vuser_init,Action,vuser_end说明 vuser_init 录制的一般是业务流程开始之前的初始化工作(如登录,服务器初始化) Action 录制的一般是业务流程操作的事件 vus ...

  6. Murano Weekly Meeting 2016.07.19

    Meeting time: 2016.July.19 1:00~2:00 Chairperson:  Kirill Zaitsev, from Mirantis Meeting summary: 1. ...

  7. aop动态代理 事务 threadlocal

    第一:package com.itheima.utils; import java.sql.Connection; import java.sql.SQLException; /** * 处理事务 的 ...

  8. aspx页面导出为word

    aspx页面导出为word代码: System.IO.StringWriter sw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWri ...

  9. ubuntu install fonts

    sudo apt-get install ttf-wqy-zenhei

  10. 搭建Activemq集群

    首先搭建zookeeper集群: 参考URL:  http://www.cnblogs.com/feiyun126/p/7244394.html 三台服务器:先设置hosts 10.0.0.231 n ...