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. Lvs Tun隧道模式配置

    ######## TUN是IP Tunneling ,IP隧道的简称,它将调度器收到的IP数据包封装在一个新的IP数据包中,转交给应用服务器,然后实际服务器的返回数据会直接返回给用户. 工作原理: 用 ...

  2. Quartz中Cron详解

    Quartz中的cron跟Linux系统的cron定义不太一样(Linux从分开始) 特殊字符: * 用来表示包含一个范围内的任意值. 例如, 分钟位置的“*” 表示 “每分钟”. ?  当不特定指代 ...

  3. spring boot http2

    项目已启动的情况下,启动单元测试报端口has binded 可以使用server.port=-1 禁用端口绑定来启动单元测试 https://docs.spring.io/spring-boot/do ...

  4. Forbidden You don't have permission to access XXX on this server

    Forbidden You don't have permission to access / on this server. 找到 apache 配置文件 httpd.conf 把里面的 <D ...

  5. 这是通过 Open Live Writer(是个博客编辑器) 发布的

    Open Live Writer  是开源的win10上的博客编辑器

  6. speex编译

    首先去官网 https://www.speex.org/downloads/ 下载解压 将include.libspeex文件夹复制到自己新建工程的jni目录下 speex有关的类 package c ...

  7. Kudu-Impala集成特性

    不多说,直接上干货! Kudu-Impala 集成特性 CREATE / ALTER / DROP TABLE Impala 支持使用 Kudu 作为持久层来 creating(创建),alterin ...

  8. linux 防止ssh暴力破解密码

    收集 /var/log/secure 里面的信息,若是某个IP 链接次数超过一定次数 ,则把此ip记录到/etc/hosts.deny里面 #!/bin/bash #Denyhosts SHELL S ...

  9. Prestashop后台模块(中英转译)

    =======================Prestashop-Modules========================= Pretashop运行起来非常慢,这个大家应该都知道,最近要给这个 ...

  10. Session有什么重大BUG,有什么方法可以解决

    [考点]ASP.NET中Session的多种保存方法.[出现频率]★★★☆☆[解答]使用进程内会话状态模式时,如果aspnet_wp.exe或应用程序域重新启动,则会话状态数据将丢失.可以用Sate ...