最基础的、最实用的、开箱即开发的基于Java的MongoDB业务开发教程,是本人初次使用时,一点一点的学习过程的整合。绝对适合初学者、自学者,开发过程中遇到的问题与解决方案,都写了进去。
傻瓜式教程!开箱即用!
免费下载地址:
https://download.csdn.net/download/luck_jinwei/12467717
以下为部分内容:
1、MongoDB库中存放的数据很多时候都是GMT时间格式,
如:Tue Feb 18 16:34:06 GMT+08:00 2020 ,当我们需要转换为常用的Date时间格式的时候:
String date = "Tue Feb 18 16:34:06 GMT+08:00 2020";
Date date = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH).parse(date
2、在使用mongoTemplate多条件查询时,需要注意:
//筛选条件,同字段多条件必须用.andOperator()
Criteria c = new Criteria();
c.andOperator(c.where("order.gmtCreate").gte(ds),
c.where("order.gmtCreate").lt(de));
//指定查询返回字段,
String[] project = {"userId", "prodId", "total_count"};
//分组查询,注意此处的通道概念。
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(c),
Aggregation.group("order.userId", "order.prodId")
.first("order.userId").as("userId")
.first("order.prodId").as("prodId")
.count().as("total_count"),
Aggregation.project(project),
Aggregation.sort(new Sort(Sort.Direction.ASC, "userId")),
Aggregation.sort(new Sort(Sort.Direction.ASC, "prodId"))
);
AggregationResults<OrderEntity> outputType
= mongoTemplate.aggregate(agg, collectionName, OrderEntity.class);
List<OrderEntity> list = outputType.getMappedResults();
4、多条件复杂查询
例如sql查询:
<select id="findList" resultType="com.exploit.domain.DataConsumerOrder"
parameterType="com.exploit.domain.DataConsumerOrder">
SELECT
T1.*, T2.user_realname,T3.prod_name
FROM
data_consumer_order T1
LEFT JOIN data_user T2 ON T1.user_id = T2.id
LEFT JOIN data_product T3 ON T1.prod_id = T3.id
<trim prefix="where " prefixOverrides="and ">
<if test="@com.zax.common.Ognl@isNotEmpty(entity.order_no)">
and T1.order_no = #{entity.order_no}
</if>
<if test="@com.zax.common.Ognl@isNotEmpty(entity.state)">
and T1.state = #{entity.state}
</if>
<if test="@com.zax.common.Ognl@isNotEmpty(entity.start_time)">
and date_format(T1.gmt_create,'%Y-%m-%d') >= #{entity.start_time}
</if>
<if test="@com.zax.common.Ognl@isNotEmpty(entity.end_time)">
and date_format(T1.gmt_create,'%Y-%m-%d') <= #{entity.end_time}
</if>
<if test="@com.zax.common.Ognl@isNotEmpty(entity.prod_id)">
and T1.prod_id = #{entity.prod_id}
</if>
<if test="@com.zax.common.Ognl@isNotEmpty(entity.process_time)">
and T1.process_time > #{entity.process_time}
</if>
<if test="@com.zax.common.Ognl@isNotEmpty(entity.use_code)">
and (T2.user_realname like concat('%',#{entity.use_code},'%')
or T3.prod_code like concat('%',#{entity.use_code},'%'))
</if>
</trim>
ORDER BY T1.gmt_create desc
</select>
............