mongo聚合

db.getCollection("es_product_notify").aggregate([
    {
        $lookup: {
            from: 'es_app_product',
            localField: 'product',
            foreignField: '_id',
            as: 'p'
        }
    },
    {
        $match:
            { 'createdDate': { $gte: ISODate('2019-06-01T00:00:00.000+0000'), $lt: ISODate('2019-07-01T00:00:00.000+0000') } }
    },
    { $group: { "_id": "$p.sn", count: { $sum: 1 } } },
    { $sort: { count: -1 } }
]) 

 

 

db.getCollection("es_product_notify").aggregate([
    {
        $lookup: {
            from: 'es_app_goods_detail',
            localField: 'product',
            foreignField: 'spls.skus._id',
            as: 'k'
        }
    },
    {
        $lookup: {
            from: 'es_app_product',
            localField: 'product',
            foreignField: '_id',
            as: 'p'
        }
    },
    {
        $match:
            { 'createdDate': { $gte: ISODate('2019-12-01T00:00:00.000+0000'), $lt: ISODate('2020-01-01T00:00:00.000+0000') } }
    },
    { $group: { "_id": {sku:"$p.sn",color:"$p.colourMemo",size:"$p.size",sn:"$k.goodsSn"}, count: { $sum: 1 } }},
    { $sort: { count: -1 } }
]) 

 

 

联表查询 spu 无spl的商品

db.getCollection("es_spu").aggregate([
 {
        $lookup: {
            from: 'es_spl',
            localField: '_id',
            foreignField: 'spu',
            as: 'p'
        }
    },
  {
        $match:
            { 'p':{$size:0}  }
    }

]) 

 

 

db.getCollection("search_history").aggregate(
    [
        {
            "$match": {
                "searchHistoryValues": { $regex: /水桶/ }
            }
        },
        {
            "$unwind": "$searchHistoryValues"
        },
        { "$group": { _id: "$searchHistoryValues", count: { $sum: 1 } } },
        {
            "$sort": {
                "count": -1.0
            }
        }
    ]
);
 

mongoTemplate实现

try {
    List<AggregationOperation> operations = new ArrayList<>();
    if(StringUtils.isNotEmpty(keyword)){
        Pattern likekeyword = Pattern.compile("^.*" + keyword + ".*$", Pattern.CASE_INSENSITIVE);
        operations.add(Aggregation.match(Criteria.where("searchHistoryValues").regex(likekeyword)));
    }
    operations.add(Aggregation.unwind("$searchHistoryValues"));
    operations.add(Aggregation.group("searchHistoryValues").count().as("count"));
    operations.add(Aggregation.sort(new Sort(Sort.Direction.DESC, "count")));
    operations.add(Aggregation.limit(100));

    Aggregation aggregation = Aggregation.newAggregation(operations);

    AggregationResults<HotSearchWord> hotSearchWordsAgg = mongoTemplate.aggregate(aggregation, "search_history", HotSearchWord.class);
    List<HotSearchWord> hotSearchWords = hotSearchWordsAgg.getMappedResults();
    List<String> collect = hotSearchWords.stream().filter(hotSearchWord -> hotSearchWord.getId().contains(keyword)).map(HotSearchWord::getId).collect(Collectors.toList());
    return collect;

}
catch (Exception e){
    e.printStackTrace();
    return null;
}

接收实体:

@Data
public class HotSearchWord {
    @Id
    private String id;
    private String count;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值