引入依赖
<dependency>
<groupId>org.mongodbgroupId>
<artifactId>mongo-java-driverartifactId>
<version>3.12.2version>
dependency>
初始化使用对象
// 数据库
private MongoDatabase db;
// 集合
private MongoCollection<Document> doc;
// 连接客户端(内置连接池)
private MongoClient client;
@Before
public void init() {
client = new MongoClient("127.0.0.1", 27017);
db = client.getDatabase("test");
doc = db.getCollection("forlanC");
}
新增两条数据,{“uid":1,“name”:“forlan1”,“city”:“广州“},{“uid”:2,“name”:“forlan2”,“city”:"深圳“}
Document doc1 = new Document();
doc1.append("uid", "1");
doc1.append("name", "forlan1");
doc1.append("city", "广州");
Document doc2 = new Document();
doc2.append("uid", "2");
doc2.append("name", "forlan2");
doc2.append("city", "深圳");
doc.insertMany(Arrays.asList(doc1, doc2));
查找集合forlanC所有文档
FindIterable<Document> documents = doc.find();
带条件查找city=广州的文档
Bson all = Filters.all("city", "广州");
FindIterable<Document> documents1 = doc.find(all);
正则表达式查找name以forlan开头,city为广州或深圳的文档
String regexStr = "^forlan.*";
Bson regex = Filters.regex("name", regexStr);
Bson or = Filters.or(Filters.eq("city", "广州"), Filters.eq("city", "深圳"));
Bson and = Filters.and(regex, or);
FindIterable<Document> documents2 = doc.find(and);
修改name=forlan1的文档,更新city为汕头
Bson eq = Filters.eq("name", "forlan1");// 过滤条件
Bson set = Updates.set("city", "汕头"); // 更新的内容
UpdateResult updateMany = doc.updateMany(eq, set);
删除name=forlan1的文档
Bson eq = Filters.eq("name", "forlan1");
DeleteResult deleteMany = doc.deleteMany(eq);
删除uid>=1并且uid<3的文档
Bson gt = Filters.gte("uid", 1);
Bson lt = Filters.lt("uid", 3);
Bson and = Filters.and(gt, lt);
DeleteResult deleteMany2 = doc.deleteMany(and);
定义ForlanC类
public class ForlanC {
private ObjectId id;
private Long uid;
private String name;
private String city;
// 省略set、get、toString方法。。。
}
初始化使用对象
// 数据库
private MongoDatabase db;
// 集合
private MongoCollection<ForlanC> doc;
// 连接客户端(内置连接池)
private MongoClient client;
@Before
public void init() {
//编解码器的list
List<CodecRegistry> codecResgistes = new ArrayList<>();
//list加入默认的编解码器集合
codecResgistes.add(MongoClient.getDefaultCodecRegistry());
//生成一个pojo的编解码器
CodecRegistry pojoCodecRegistry = CodecRegistries.
fromProviders(PojoCodecProvider.builder().automatic(true).build());
//list加入pojo的编解码器
codecResgistes.add(pojoCodecRegistry);
//通过编解码器的list生成编解码器注册中心
CodecRegistry registry = CodecRegistries.fromRegistries(codecResgistes);
//把编解码器注册中心放入MongoClientOptions
MongoClientOptions mongoClientOptions = MongoClientOptions.builder().codecRegistry(registry).build();
ServerAddress serverAddress = new ServerAddress("127.0.0.1", 27017);
client = new MongoClient(serverAddress, mongoClientOptions);
db = client.getDatabase("test");
doc = db.getCollection("forlanC", ForlanC.class);
}
新增两条数据,{“uid":1,“name”:“forlan1”,“city”:“广州“},{“uid”:2,“name”:“forlan2”,“city”:"深圳“}
ForlanC forlanC1 = new ForlanC();
forlanC1.setUid(1L);
forlanC1.setName("forlan1");
forlanC1.setCity("广州");
ForlanC forlanC2 = new ForlanC();
forlanC2.setUid(2L);
forlanC2.setName("forlan2");
forlanC2.setCity("深圳");
doc.insertMany(Arrays.asList(forlanC1, forlanC2));
查找集合forlanC所有文档
FindIterable<ForlanC> documents = doc.find();
带条件查找city=广州的文档
Bson all = Filters.all("city", "广州");
FindIterable<ForlanC> documents1 = doc.find(all);
正则表达式查找name以forlan开头,city为广州或深圳的文档
String regexStr = "^forlan.*";
Bson regex = Filters.regex("name", regexStr);
Bson or = Filters.or(Filters.eq("city", "广州"), Filters.eq("city", "深圳"));
Bson and = Filters.and(regex, or);
FindIterable<ForlanC> documents2 = doc.find(and);
修改name=forlan1的文档,更新city为汕头
Bson eq = Filters.eq("name", "forlan1");// 过滤条件
Bson set = Updates.set("city", "汕头"); // 更新的内容
UpdateResult updateMany = doc.updateMany(eq, set);
删除name=forlan1的文档
Bson eq = Filters.eq("name", "forlan1");
DeleteResult deleteMany = doc.deleteMany(eq);
删除uid>=1并且uid<3的文档
Bson gt = Filters.gte("uid", 1);
Bson lt = Filters.lt("uid", 3);
Bson and = Filters.and(gt, lt);
DeleteResult deleteMany2 = doc.deleteMany(and);
引入依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-mongodbartifactId>
dependency>
配置数据库
spring.data.mongodb.database=test
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=27017
注入MongoOperations对象
@Resource
private MongoOperations tempelate;
新增两条数据,{“uid":1,“name”:“forlan1”,“city”:“广州“},{“uid”:2,“name”:“forlan2”,“city”:"深圳“}
ForlanC forlanC1 = new ForlanC();
forlanC1.setName("forlan1");
forlanC1.setCity("广州");
ForlanC forlanC2 = new ForlanC();
forlanC2.setName("forlan2");
forlanC2.setCity("深圳");
tempelate.insertAll(Arrays.asList(forlanC1, forlanC2));
带条件查找city=广州的文档
Criteria all = Criteria.where("city").all(Arrays.asList("广州"));
List<ForlanC> find = tempelate.find(Query.query(all), ForlanC.class);
正则表达式查找name以forlan开头,city为广州或深圳的文档
String regexStr = "forlan.*";
Criteria regex = Criteria.where("name").regex(regexStr);
Criteria or1 = Criteria.where("city").all("广州");
Criteria or2 = Criteria.where("city").all("深圳");
Criteria or = new Criteria().orOperator(or1, or2);
Query query = Query.query(new Criteria().andOperator(regex, or));
List<ForlanC> find2 = tempelate.find(query, ForlanC.class);
修改name=forlan1的文档,更新city为汕头
Query query = Query.query(Criteria.where("name").is("forlan1"));
Update update = Update.update("city", "汕头");
UpdateResult updateFirst = tempelate.updateMulti(query, update, ForlanC.class);
删除name=forlan1的文档
Query query1 = Query.query(Criteria.where("name").is("forlan1"));
DeleteResult remove = tempelate.remove(query1, ForlanC.class);
删除uid>=1并且uid<3的文档
Query query2 = Query.query(new Criteria().andOperator(Criteria.where("uid").gte(1), Criteria.where("uid").lt(3)));
DeleteResult remove2 = tempelate.remove(query2, ForlanC.class);
出现错误
java.lang.NoSuchMethodError: com.mongodb.client.MongoCollection.insertMany(Ljava/util/List;)Lcom/mongodb/client/result/InsertManyResult;
原因
pom.xml 文件存在两个不同版本的依赖,版本低的可能无法正常操作MongoDB数据库
解决
删除一个依赖,保留我们需要的版本
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-mongodbartifactId>
dependency>