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. 简单的Web日志分析脚本

    前言 长话短说,事情的起因是这样的,由于工作原因需要分析网站日志,服务器是windows,iis日志,在网上找了找,github找了找,居然没找到,看来只有自己动手丰衣足食. 那么分析方法我大致可分为 ...

  2. Windows无法启动 VMware Workstation server服务解决方法

    Windows无法启动VMware Workstation server服务, 可以通过删除datastores.xml文件来解决. 具体操作步骤如下: 1.在系统盘目录下,找到C:\ProgramD ...

  3. PHP rand 和 mt_rand

    PHP mt_rand() 函数 定义和用法 mt_rand() 使用 Mersenne Twister 算法返回随机整数. 语法 mt_rand(min,max) 说明 如果没有提供可选参数 min ...

  4. JavaWeb xss攻击

    出处: http://www.cnblogs.com/TankXiao/archive/2012/03/21/2337194.html XSS 全称(Cross Site Scripting) 跨站脚 ...

  5. css预处理器 sass和stylus对比以及常用功能

    在众多的css预处理器语言中,sass和stylus算是十分优秀的两个.本文主要针对两者的常用功能做个简单的对比分析.在对比中了解二者的差异,同时帮助大家更好的掌握这两种预处理语言. 本文涉及到的sa ...

  6. STL中的算法

    STL中的所有算法(70个) 参考自:http://www.cppblog.com/mzty/archive/2007/03/14/19819.htmlhttp://hi.baidu.com/ding ...

  7. Android NDK开发 Jni中Debug(三)

    下载LLDB 配置Android Native - Debugger 调式结果如下 #include <jni.h> #include <string.h> #include& ...

  8. public class 与 class 的区别

    public class 与 class 的区别 1.一个类前面的public是可有可无的 2.如果一个类使用 public 修饰,则文件名必须与类名一致 3.如果一个类前面没有使用public修饰, ...

  9. visual stdio使用

    现在转换使用visual stdio,因为很多和以前的快捷键不同,也不打算换了,这样可移动性应该更好吧. 1.注释代码, 首先按下ctrl + k,然后再按下ctrl + c 2.取消注释,首先按下c ...

  10. inventor安装错误

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...