MongoDB常用API
目录1、引入pom2、访问设置3、常用API1、引入pom<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId></dependency>2、访问设置1)mo
·
1、引入pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2、访问设置
1)mongoDB默认只要本机可以访问,先开放客户端连接权限
./mongod --config /usr/local/mongo/etc/mongo0.conf --bind_ip 0.0.0.0
2)本机连接虚拟机时,需要开开放防火墙端口
firewall-cmd --zone=public --add-port=37010/tcp --permanent
firewall-cmd --reload;
3、常用API
package com.example.demo.utils;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import java.util.regex.Pattern;
/**
* @Author: pandafox
* @Desctription: TODO
* @Date: Created in 2020/7/19 17:16
* @Version: 1.0
*/
public class TestMgdb {
public static void main(String[] args) {
//1.创建连接
MongoClient client = new MongoClient("192.168.48.128", 37010);
//2.数据库连接
MongoDatabase database = client.getDatabase("test");
//3.创建集合
// database.createCollection("api_col01");
//4.查询集合
MongoCollection<Document> col01 = database.getCollection("api_col01");
//5.写入文档
// Random r = new Random();
// List<Document> docs = new ArrayList<>();
// for (int i = 0; i < 10; i++) {
// Document doc = new Document();
// doc.append("name", "name" + i);
// doc.append("age", r.nextInt(50));
// docs.add(doc);
// // collection.insertOne(doc); // 单一写入
// }
// col01.insertMany(docs); // 批量写入
//6.查询所有
FindIterable<Document> result = col01.find();
MongoCursor<Document> cursor = result.iterator();
// while (cursor.hasNext()) {
// Document doc = cursor.next();
// System.out.print("find all : [ { doc : " + doc + " }, ");
// System.out.print("{ name : " + doc.getString("name") + " }, ");
// System.out.println("{ age : " + doc.get("age") + " } ]");
// }
// 7.更新文档
// col01.updateMany(Filters.eq("name", "name0"), new Document("$set", new Document("name", "name100")));
// result = col01.find();
// cursor = result.iterator();
// while (cursor.hasNext()) {
// Document doc = cursor.next();
// System.out.print("update find all : [ { doc : " + doc + " }, ");
// System.out.print("{ name : " + doc.getString("name") + " }, ");
// System.out.println("{ age : " + doc.get("age") + " } ]");
// }
// 8.删除文档
// col01.deleteOne(Filters.eq("name", "name100"));
// col01.deleteMany(Filters.lt("age", 30));
// result = col01.find();
// cursor = result.iterator();
// while (cursor.hasNext()) {
// Document doc = cursor.next();
// System.out.print("delete find all : [ { doc : " + doc + " }, ");
// System.out.print("{ name : " + doc.getString("name") + " }, ");
// System.out.println("{ age : " + doc.get("age") + " } ]");
// }
// 9.条件查询
// 查询name = name3的
result = col01.find(Filters.eq("name", "name3"));
cursor = result.iterator();
while (cursor.hasNext()) {
Document doc = cursor.next();
System.out.print("eq : [ { doc : " + doc + " }, ");
System.out.print("{ name : " + doc.getString("name") + " }, ");
System.out.println("{ age : " + doc.get("age") + " } ]");
}
// 查询age > 30的
result = col01.find(Filters.gt("age", 30));
// result = collection.find(new Document("age", new Document("$gt", 30)));
cursor = result.iterator();
while (cursor.hasNext()) {
Document doc = cursor.next();
System.out.print("gt : [ { doc : " + doc + " }, ");
System.out.print("{ name : " + doc.getString("name") + " }, ");
System.out.println("{ age : " + doc.get("age") + " } ]");
}
// 查询name以n开头,以9结尾的
result = col01.find(Filters.regex("name", Pattern.compile("^n.*9$")));
cursor = result.iterator();
while (cursor.hasNext()) {
Document doc = cursor.next();
System.out.print("regex : [ { doc : " + doc + " }, ");
System.out.print("{ name : " + doc.getString("name") + " }, ");
System.out.println("{ age : " + doc.get("age") + " } ]");
}
// 查询name = name0 并且 age < 50的
result = col01.find(Filters.and(Filters.eq("name", "name0"), Filters.lt("age", 50)));
cursor = result.iterator();
while (cursor.hasNext()) {
Document doc = cursor.next();
System.out.print("and query : [ { doc : " + doc + " }, ");
System.out.print("{ name : " + doc.getString("name") + " }, ");
System.out.println("{ age : " + doc.get("age") + " } ]");
}
// 查询name = name0 或者 age < 50的
result = col01.find(Filters.or(Filters.eq("name", "name0"), Filters.lt("age", 50)));
cursor = result.iterator();
while (cursor.hasNext()) {
Document doc = cursor.next();
System.out.print("or query : [ { doc : " + doc + " }, ");
System.out.print("{ name : " + doc.getString("name") + " }, ");
System.out.println("{ age : " + doc.get("age") + " } ]");
}
}
}
package com.example.demo.utils;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.Arrays;
/**
* @Author: pandafox
* @Desctription: TODO
* @Date: Created in 2020/7/19 21:57
* @Version: 1.0
*/
public class TestMgdb2 {
public static void main(String[] args) {
try {
// 连接池
MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
builder.connectTimeout(5000); // 连接超时
builder.socketTimeout(5000); // 读写超时
builder.connectionsPerHost(30); // 每个地址的最大连接数
// 提供用户名和密码
// MongoCredential credential = MongoCredential.createScramSha1Credential("username", "databaseName", "password".toCharArray());
// 集群访问, 请求地址传入多个
MongoClient client = new MongoClient(Arrays.asList(
new ServerAddress("192.168.48.128", 37010)
// new ServerAddress("192.168.48.128", 37011),
// new ServerAddress("192.168.48.128", 37012)
), builder.build());
// 连接时校验用户
// MongoClient client = new MongoClient(Arrays.asList(new ServerAddress("192.168.48.128", 37010)), credential, builder.build());
MongoDatabase database = client.getDatabase("test");
MongoCollection<Document> col01 = database.getCollection("api_col01");
FindIterable<Document> result = col01.find().limit(2).skip(2).sort(new Document("age", 1));
System.out.println(result);
MongoCursor<Document> cursor = result.iterator();
// while (cursor.hasNext()) {
// Document doc = cursor.next();
// System.out.print("find all : [ { doc : " + doc + " }, ");
// System.out.print("{ name : " + doc.getString("name") + " }, ");
// System.out.println("{ age : " + doc.get("age") + " } ]");
// }
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
}
}
更多推荐
已为社区贡献2条内容
所有评论(0)