package _0605;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.client.*;
import com.mongodb.client.model.Filters;
import com.mongodb.client.result.DeleteResult;
import org.apache.commons.collections.iterators.FilterIterator;
import org.bson.Document;
import org.bson.conversions.Bson;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;
/**
* @Author Xulihua
* @Date2021/6/5
* @Description
*/
public class mongoTest {
public static void main(String[] args) {
String mongohost = "192.168.107.103";
int mongoport = 27017;
MongoClient mongoClient = new MongoClient(mongohost, mongoport);
//查看所有库
MongoIterable<String> dbs = mongoClient.listDatabaseNames();
// for (String db : dbs) {
// System.out.println(db);
// }
//查看库具体信息
ListDatabasesIterable<Document> documents = mongoClient.listDatabases();
// for (Document document : documents) {
// System.out.println(document);
// }
//删库
// mongoClient.dropDatabase("June_Five");
//建库
MongoDatabase june_five = mongoClient.getDatabase("June_Five");
//建表
// june_five.createCollection("test");
// june_five.createCollection("test2");
// Document document = new Document();
// document.append("title","aa").append("content","bb").
// append("name","cc").append("userid","0001").append("nick","kk");
// june_five.getCollection("tableOne").insertOne(document);
// 查看June_Five库是否建成
MongoIterable<String> dbs2 = mongoClient.listDatabaseNames();
// for (String s : dbs2) {
// System.out.println(s);
// }
// 删除表 tableOne
// june_five.getCollection("tableTwo").drop();
// 查看June_Five库里的表tableOne是否建成
// MongoIterable<String> tables = june_five.listCollectionNames();
// for (String table : tables) {
// System.out.println(table);
// }
//查看表内容
// FindIterable<Document> tableOne = june_five.getCollection("tableOne").find();
// for (Document document2 : tableOne) {
// System.out.println(document2);
// }
// 查看表中数据条数
// long cnt = june_five.getCollection("tableOne").count();
// System.out.println(cnt);
// 每个document对象append一条数据
// Document t1 = new Document();
// t1.append("_id", "1").append("title", "aa").append("content", "good").append("readNum", "21")
// .append("name", "a1").append("userid", "0001").append("nick", "gree");
// Document t2 = new Document();
// t2.append("_id", "2").append("title", "bb").append("content", "hi").append("readNum", "28")
// .append("name", "b1").append("userid", "0002").append("nick", "ant");
// Document t3 = new Document();
// t3.append("_id", "3").append("title", "cc").append("content", "ok").append("readNum", "27")
// .append("name", "c1").append("userid", "0003").append("nick", "pan");
// Document t4 = new Document();
// t4.append("_id", "4").append("title", "dd").append("content", "no").append("readNum", "29")
// .append("name", "a1").append("userid", "0001").append("nick", "gree");
// Document t5 = new Document();
// t5.append("_id", "5").append("title", "ee").append("content", "yes").append("readNum", "22")
// .append("name", "d1").append("userid", "0005").append("nick", "dog");
//把多个document对象 放到list里
// List<Document> list=new ArrayList<>();
// list.add(t1);
// list.add(t2);
// list.add(t3);
// list.add(t4);
// list.add(t5);
//插入数据
// june_five.getCollection("tableTwo").insertMany(list);
MongoCollection<Document> tableTwo = june_five.getCollection("tableTwo");
// FindIterable<Document> documents1 = tableTwo.find();
// for (Document document : documents1) {
// System.out.println(document);
// }
//
//按照1个条件查询数据
// FindIterable<Document> documents2 = tableTwo.find(Filters.eq("name", "a1"));
// for (Document document : documents2) {
// System.out.println(document);
// }
// 多条件 查询指定字段
// FindIterable<Document> res = tableTwo.find(Filters.eq("name","a1")).filter(Filters.eq("title","aa"))
// .projection(new BasicDBObject("title",1).append("name",1).append("_id",0));
// for (Document re : res) {
// System.out.println(re);
// }
// tableTwo.find(Filters.eq())
//删除指定条件的数据
// DeleteResult deleteResult = tableTwo.deleteOne(Filters.eq("name", "a1"));
// FindIterable<Document> res = tableTwo.find();
// for (Document re : res) {
// System.out.println(re);
// }
//多条件 删除数据 document是Bson接口的实现类
// Document document = new Document();
// document.append("_id","2").append("readNum","28");
// tableTwo.deleteOne(document);
// FindIterable<Document> documents1 = tableTwo.find();
// for (Document document1 : documents1) {
// System.out.println(document1);
// }
// Document document = new Document();
// document.append("_id","3");
// tableTwo.deleteMany(document);
// for (Document document1 : tableTwo.find()) {
// System.out.println(document1);
// }
// k跨两行 查两行 第 3 4 行
// FindIterable<Document> res = tableTwo.find().limit(2).skip(2);
// for (Document re : res) {
// System.out.println(re);
// }
// 对查询的结果 升序 1 倒叙 -1
// Document document = new Document();
// document.append("readNum",-1);
// FindIterable<Document> res = tableTwo.find().sort(document);
// for (Document re : res) {
// System.out.println(re);
// }
//查询阅读量最高的作者
// FindIterable<Document> res = tableTwo.find().projection(new BasicDBObject().append("name", 1))
// .sort(Filters.eq("readNum", -1)).limit(1);
// for (Document re : res) {
// System.out.println(re);
// }
//按userid升序 同样的userid 再按照readNum 降序
// Document document = new Document();
// document.append("userid",1).append("readNum",-1);
// FindIterable<Document> res = tableTwo.find().sort(document );
// for (Document re : res) {
// System.out.println(re);
// }
// 查询content 包含 "o" 的
// Pattern pattern = Pattern.compile("o");
// BasicDBObject obj = new BasicDBObject();
// obj.put("content", pattern);
// FindIterable<Document> res = tableTwo.find(obj);
// for (Document re : res) {
// System.out.println(re);
// }
//查询content 以 "o" 开头的
// Pattern pattern = Pattern.compile("^o");
// BasicDBObject obj = new BasicDBObject();
// obj.put("content", pattern);
// FindIterable<Document> res = tableTwo.find(obj);
// for (Document re : res) {
// System.out.println(re);
// }
//查询readNum阅读量 大于25 小于29 的数据
// FindIterable<Document> res = tableTwo.find(Filters.lt("readNum", "29")).filter(Filters.gt("readNum","25"));
// for (Document re : res) {
// System.out.println(re);
// }
//查询readNum阅读量 大于25 小于30 作者名字为a1 的数据
// FindIterable<Document> res = tableTwo.find(Filters.and(Filters.gt("readNum", "25"),Filters.lt("readNum","30"),Filters.eq("name","a1"))) ;
// for (Document re : res) {
// System.out.println(re);
// }
//查询userid为 0001 和 0002 的数据 $in
// FindIterable<Document> res = tableTwo.find(Filters.in("userid", "0001", "0002"));
// for (Document re : res) {
// System.out.println(re);
// }
//查询readNum阅读量 大于21 并且 小于25 $and
// FindIterable<Document> res = tableTwo.find(Filters.and(Filters.gt("readNum", "21"), Filters.lt("readNum", "25")));
// for (Document re : res) {
// System.out.println(re);
// }
//查询name为c1 或者 readNum 小于25 $or
// FindIterable<Document> res = tableTwo.find(Filters.or(Filters.lt("readNum", "25"), Filters.eq("name", "c1")));
// for (Document re : res) {
// System.out.println(re);
// }
//将 name 为a1 的那条数据 content 值改为kkk 只修改一条
tableTwo.updateOne(Filters.eq("name","a1"),new Document("$set",new Document("content","ko")));
// for (Document document : tableTwo.find()) {
// System.out.println(document);
// }
//将 name 为a1 的那条数据 content 值改为koko 修改多条
// tableTwo.updateMany(Filters.eq("name","a1"),new Document("$set",new Document("content","koko")));
// FindIterable<Document> documents1 = tableTwo.find();
// for (Document document : documents1) {
// System.out.println(document);
// }
//查看索引
// ListIndexesIterable<Document> res = tableTwo.listIndexes();
// for (Document re : res) {
// System.out.println(re);
// }
// tableTwo.createIndex(Filters.eq("userid",1));
//
// 删除指定索引
// tableTwo.dropIndex("userid_1");
//删除所有索引 _id 不能被删除
tableTwo.dropIndexes();
// 查看索引
ListIndexesIterable<Document> res = tableTwo.listIndexes();
for (Document re : res) {
System.out.println(re);
}
}
}
mongodb 的java代码的常用增删改查等操作
最新推荐文章于 2024-04-30 14:10:42 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)