mongod java轻松入门,详解系列文章

本文通过Java代码展示了如何在MongoDB数据库中进行增删改查操作,包括查询特定条件的文档、插入新记录、更新数据和删除文档,同时提及了相关的技术点如JVM、MySQL等。
摘要由CSDN通过智能技术生成

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)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值