java 使用mongodb
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的更多相关文章
- 【MongoDB for Java】Java操作MongoDB
上一篇文章: http://www.cnblogs.com/hoojo/archive/2011/06/01/2066426.html介绍到了在MongoDB的控制台完成MongoDB的数据操作,通过 ...
- JAVA操作MongoDB数据库
1. 首先,下载MongoDB对Java支持的驱动包 驱动包下载地址:https://github.com/mongodb/mongo-java-driver/downloads 2.Java操作Mo ...
- [转]MongoDB for Java】Java操作MongoDB
原文地址: MongoDB for Java]Java操作MongoDB 开发环境: System:Windows IDE:eclipse.MyEclipse 8 Database:mongoDB 开 ...
- Java 连接MongoDB
1.驱动 通过java连接MongoDB需要一个java版的驱动 下载地址:http://mongodb.github.io/mongo-java-driver/ 2.连接MongoDB 通过 com ...
- Java操作MongoDB
上一篇文章: http://www.cnblogs.com/hoojo/archive/2011/06/01/2066426.html 介绍到了在MongoDB的控制台完成MongoDB的数据操作,通 ...
- mongDB基本命令和Java操作MongoDB
上一篇博文<mongoDB安装>我们安装了mongoDB,现在来复习一下它的一些基本命令:mongoDB的bin目录加入到path之后,命令行中输入mongo: 然后我们进入正题 1.查看 ...
- Mongodb快速入门之使用Java操作Mongodb
[IT168 专稿]在上一篇文章中,我们学习了Mongodb的安装和初步使用,在本文中,将学习如何使用Java去编程实现对Mongodb的操作. HelloWorld程序 学习任何程序的第一步,都是编 ...
- Mongodb入门并使用java操作Mongodb
转载请注意出处:http://blog.csdn.net/zcm101 最近在学习NoSql,先从Mongodb入手,把最近学习的总结下. Mongodb下载安装 Mongodb的下载安装就不详细说了 ...
- 浅谈如何用Java操作MongoDB
NoSQL数据库因其可扩展性使其变得越来越流行,利用NoSQL数据库可以给你带来更多的好处,MongoDB是一个用C++编写的可度可扩展性的开源NoSQL数据库.本文主要讲述如何使用Java操作Mon ...
- Java实现mongodb原生增删改查语句
Java实现mongodb原生增删改查语句 2018-03-16 自动化测试时,需校验数据库数据,为了快速自动化,在代码中用原生增删改查语句操作mongodb 结构 代码 0 pom.xml < ...
随机推荐
- linux中firewall与iptables防火墙服务
火墙firewall-cmd --state 查看火墙的状态firewall-cmd --get-active-zones 目前所处的域firewall-cmd --get-default-zone ...
- Array to List
List<OisDiscountIndex> discountIndexes = Arrays.asList(new OisDiscountIndex[trades.size()]);
- UBoot添加命令的方法
1. 具体实现步骤 ① 在./common文件夹下新建cmd_led.c,并在此文件中添加如下内容 #include <common.h> #include <command.h&g ...
- py---------常用模块
一.认识模块? 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1. 使用pytho ...
- docker 部署公司阿里云服务器 (一)
持续更新... 背景环境: 阿里云ecs服务器 centos7.4 公网地址:xx.xx.xx.xx 内网地址:172.16.77.4 阿里云RDS 阿里云 Redis 第 ...
- 分配一维动态数组or 二维动态数组的方法以及学习 new 方法or vector
先来个开胃菜 // 使用new动态分配存储空间 #include<iostream> using std::cout; int main() { // 第1种方式 int *a=new i ...
- python csv.reader参数指定
- Android中当前Activity跳转到当前Activity页面
步骤:先关闭自己,在跳转 case R.id.btn_copy:// 复制 Toast.makeText(mContext, "正在复制", Toast.LENGTH_SHORT) ...
- (转)sdd for aix 安装及基本命令
总结出自多个文件(自己做的项目和网上找的资料) 原文:http://blog.csdn.net/yujin2010good/article/details/11395701 一.sddpcm安装 要安 ...
- pat1016. Phone Bills (25)
1016. Phone Bills (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A long-di ...