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