import java.util.function.Consumer;
import static com.mongodb.client.model.Filters.*;
/**
* mongodb入门示例
* 增删改查
*/
public class MongodbDemo {
private MongoCollection mongoCollection;
public static void main(String[] args) {
}
@Before
public void init(){
// 建立连接
MongoClient mongoClient = MongoClients.create(“mongodb://192.168.168.156:27200”);
// 选择数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase(“testdb”);
// 选择表
this.mongoCollection = mongoDatabase.getCollection(“user”);
}
// 查询age<=50并且id>=100的用户信息,并且按照id倒序排序,只返回id,age,name,sex字段,不返回_id字段
@Test
public void testQuery(){
this.mongoCollection.find(
and(
lte(“age”, 50),
gte(“id”, 100)
) )
.sort(Sorts.descending(“id”))
.projection(
Projections.fields(
Projections.include(“id”,“age”,“name”,“sex”,“wealth”),
Projections.excludeId()
) )
.forEach((Consumer<? super Document>) document -> {
System.out.println(document.toJson());
}); ;
}
@Test
public void testInsert() 需要zi料+ 绿色徽【vip1024b】
{
Document document = new Document(“id”,10001)
.append(“name”, “张三丰”)
.append(“age”, 15)
.append(“sex”,“male”)
.append(“wealth”,“1000w RMB”);
this.mongoCollection.insertOne(document); System.out.println(“插入数据成功!”);
this.mongoCollection.find(eq(“id”, 10001))
.forEach((Consumer<? super Document>) doc->{
System.out.println(doc.toJson());
});
}
@Test
public void testUpdate(){
UpdateResult updateResult = this.mongoCollection
.updateOne(eq(“id”, 10001), Updates.set(“age”, 18));
System.out.println(updateResult);
this.mongoCollection.find(eq(“id”, 10001))
.forEach((Consumer<? super Document>) doc->{
});
}
@Test
public void testDelete(){
DeleteResult deleteResult = this.mongoCollection.deleteOne(eq(“id”,
10001));
最后
分享一些系统的面试题,大家可以拿去刷一刷,准备面试涨薪。
这些面试题相对应的技术点:
- JVM
- MySQL
- Mybatis
- MongoDB
- Redis
- Spring
- Spring boot
- Spring cloud
- Kafka
- RabbitMQ
- Nginx
- …
大类就是:
- Java基础
- 数据结构与算法
- 并发编程
- 数据库
- 设计模式
- 微服务
- 消息中间件
Nn-1710361572803)]
[外链图片转存中…(img-tkMBIqNI-1710361572803)]
[外链图片转存中…(img-15yCCRGt-1710361572804)]
[外链图片转存中…(img-5FHiCuE9-1710361572804)]
[外链图片转存中…(img-vJOyYuKn-1710361572804)]