springboot系列-mongodb

mongodb 安装 参考 https://blog.csdn.net/wenwang3000/article/details/99319517
1.maven依赖

	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-mongodb</artifactId>
		</dependency>

2.创建数据库用户

db.createUser(
   {
     user: "liuli",
     pwd: "123456",
     
     roles: [{"role":"readWrite","db":"mongo"}],
    /* All built-in Roles 
     Database User Roles: read|readWrite
     Database Admin Roles: dbAdmin|dbOwner|userAdmin
     Cluster Admin Roles: clusterAdmin|clusterManager|clusterMonitor|hostManager
     Backup and Restoration Roles: backup|restore
     All-Database Roles: readAnyDatabase|readWriteAnyDatabase|userAdminAnyDatabase|dbAdminAnyDatabase
     Superuser Roles: root 
    */
    
    // authenticationRestrictions: [ {
    //     clientSource: ["192.168.0.0"],
    //     serverAddress: ["xxx.xxx.xxx.xxx"]
    //  } ],

    //mechanisms: [ "<SCRAM-SHA-1|SCRAM-SHA-256>", ... ], 

    //passwordDigestor: "<server|client>"
   }
)

3.配置文件(application.yml)

spring:
  application:
    name: multiple-datasource-mongo
  data:
    mongodb:
      # 单机模式 (如数据库部署在内网环境,建议不使用密码)
      uri: mongodb://liuli:123456@localhost:27017/mongo
      # 无密码
      #  uri: mongodb://localhost:27017/mongo
      # 集群模式
      # mongodb://user:pwd@ip1:port1,ip2:port2/database  集群

  1. 单一数据源
    在这里插入图片描述
    参考:
    https://github.com/472732787/com-spring-multiple-datasource/tree/master/multiple-datasource-mongo

  2. 多数据源
    在这里插入图片描述
    参考:
    https://github.com/472732787/com-spring-multiple-datasource/tree/master/multiple-datasource-mongo-multiple



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于构建基于Spring框架的应用程序的工具,而MongoDB是一种非关系型数据库。在Spring Boot应用程序中使用MongoDB进行分组查询非常简单,可以通过使用Spring Data MongoDB来实现。 首先,你需要在你的pom.xml文件中添加Spring Data MongoDB的依赖。然后,你需要创建一个MongoDB配置类,来配置MongoDB连接。 接下来,在你的Spring Boot应用程序中创建一个模型类,并使用@Document注解来指定该类将被映射到MongoDB集合。然后,你可以使用@Field注解来指定该类中的字段将被映射到MongoDB文档中的哪个字段。 对于分组查询,你可以使用Spring Data MongoDB提供的Aggregation框架。使用Aggregation框架,你可以构建一个管道(pipeline),其中包含一系列操作符(operators),用于执行各种聚合操作。例如,你可以使用$group操作符将文档按某个字段进行分组,并计算每个分组中文档的数量或其他统计数据。 下面是一个示例代码片段,展示了如何使用Spring Data MongoDB进行分组查询: ``` @Repository public class UserRepository { @Autowired private MongoTemplate mongoTemplate; public List<GroupResult> groupByAge() { Aggregation aggregation = Aggregation.newAggregation( Aggregation.group("age").count().as("count"), Aggregation.project("count").and("age").previousOperation() ); AggregationResults<GroupResult> groupResults = mongoTemplate.aggregate(aggregation, "user", GroupResult.class); return groupResults.getMappedResults(); } } @Data @NoArgsConstructor @AllArgsConstructor class GroupResult { private Integer age; private Long count; } ``` 在这个示例中,我们首先使用group操作符按照年龄字段进行分组,并计算每个分组中文档的数量。然后,我们使用project操作符来选择返回结果中的字段。最后,我们通过调用MongoTemplate的aggregate方法执行聚合操作,并将结果映射为GroupResult类的对象列表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值