环境
  MongoDB 3.0
  CentOS6.5_x64

相关jar:mongo-java-driver-3.0.0.jar

package com.mongodb;

import java.util.HashMap;
import java.util.Map; import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId; import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase; public class TestMongoDB { public static void main(String[] args)
{
TestMongoDB test = new TestMongoDB();
//插入数据
test.insertData();
//查询数据
test.find();
test.findone();
//更新数据
test.update();
//删除数据
test.delete();
} public void find()
{
//创建连接客户端
MongoClient client = new MongoClient("localhost",27017);
//获取数据库对象
MongoDatabase db = client.getDatabase("test");
//获取操作的集合对象
MongoCollection<Document> collection = db.getCollection("wjy");
//具体操作
FindIterable<Document> it = collection.find();
//获取游标对象
MongoCursor<Document> cursor = it.iterator();
while (cursor.hasNext())
{
//取出每一个文档对象(行)
Document doc = cursor.next();
String name = doc.getString("name");
if ("乔峰".equals(name))
{
Integer age = doc.getInteger("age");
System.out.println(name+","+age);
}
else
{
Double age = doc.getDouble("age");
System.out.println(name+","+age);
} }
//释放资源
cursor.close();
client.close();
} public void findone()
{
MongoClient client = new MongoClient("localhost",27017);
//获取数据库对象
MongoDatabase db = client.getDatabase("test");
//获取操作的集合对象
MongoCollection<Document> collection = db.getCollection("wjy");
//具体操作
Bson filter = new BasicDBObject("_id", new ObjectId("5850eacd7065f52b0eab7ff4"));
FindIterable<Document> it = collection.find(filter);
//获取游标对象
MongoCursor<Document> cursor = it.iterator();
while (cursor.hasNext())
{
//取出每一个文档对象(行)
Document doc = cursor.next();
String name = doc.getString("name");
if ("乔峰".equals(name))
{
Integer age = doc.getInteger("age");
System.out.println(name+","+age);
}
else
{
Double age = doc.getDouble("age");
System.out.println(name+","+age);
} }
//释放资源
cursor.close();
client.close();
} public void insertData()
{
MongoClient client = new MongoClient("localhost",27017);
MongoDatabase db = client.getDatabase("test");
MongoCollection<Document> collection = db.getCollection("wjy");
//使用Map来封装json数据
Map<String,Object> map = new HashMap<String,Object>();
map.put("name", "乔峰");
map.put("age", 35);
map.put("gender", "true man");
Map<String,Object> hobbyMap = new HashMap<String,Object>();
hobbyMap.put("girl", "阿朱");
hobbyMap.put("gongfu", "降龙十八掌");
map.put("hobby", hobbyMap);
Document doc = new Document(map); collection.insertOne(doc); client.close();
} public void update()
{
MongoClient client = new MongoClient("localhost",27017);
MongoDatabase db = client.getDatabase("test");
MongoCollection<Document> collection = db.getCollection("wjy"); Bson filter = new BasicDBObject("_id",new ObjectId("5850eacd7065f52b0eab7ff4")); Map<String,Object> map = new HashMap<String,Object>();
map.put("name", "张无忌");
map.put("age", 35);
Bson update = new BasicDBObject(map); collection.updateOne(filter, new BasicDBObject("$set",update)); client.close();
}
public void delete()
{
MongoClient client = new MongoClient("localhost",27017);
MongoDatabase db = client.getDatabase("test");
MongoCollection<Document> collection = db.getCollection("wjy"); collection.deleteOne(new BasicDBObject("_id",new ObjectId("5850eacd7065f52b0eab7ff4"))); client.close();
}
}

参考:

https://www.w3cschool.cn/mongodb/mongodb-java.html

最新文章

  1. maven引入的jar带了时间戳
  2. bzoj 3110 K大数查询
  3. php查看网页源代码的方法
  4. sql 执行 delete 的时候,结合子查询 exists ,怎样支持别名呢?
  5. php require和include区别
  6. phonegap 百度云推
  7. 恒天云 3.0:打造基于OpenStack的私有云新模式
  8. 【Android】Android的优点和不足之处
  9. C#后台找不到前台html标签
  10. TCP传输连接建立与释放详解
  11. Illegal resource reference: @*android resources are private and not always present
  12. 转:LoadRunner自带的协议分析工具
  13. 程序设计中的数学思维函数总结(代码以C#为例)
  14. 关于linux内核驱动开发中Makefile编译的问题
  15. HTTP协议详解(三)
  16. vue diff 算法学习
  17. 爬虫——scrapy框架
  18. delphi 原创应用工具箱
  19. 类之间关系理解:组合&gt;聚合&gt;关联&gt;依赖;实现,继承
  20. 【Cf #291 B】R2D2 and Droid Army(二分,线段树)

热门文章

  1. Gym - 247731E :room(最小费用流裸题)
  2. cmds系统归并缓慢的处理过程 2017-2-16
  3. shell 编程生成日期文件;Server虚拟机上进行Web服务器配置
  4. Go语言 - 结构体 | 方法
  5. js编程思想:模型进化论--JS 的 new 到底是干什么的?
  6. 再见Spring Boot 1.x
  7. nginx之系统参数优化
  8. Mysql中联合索引的最左匹配原则(百度)
  9. 【深入学习linux】CentOS 7 最小化安装后程序必须安装的组件
  10. yum 安装指定版本Docker