2017-01-25 55 views
-2

創建春本地查詢如何從春MongoDB中創建本地查詢是這樣的:如何在MongoDB中

db.testz.aggregate([{ 
    $match: { 
     fp: { 
      $in: ['100.100', '700.700', '5000.5000', '990000.990000', '777000.777000', 
       '330000.330000', '705000.705000', '350000.350000', '800000.800000', '200000.200000' 
      ] 
     } 
    } 
    }, { 
    $group: { 
     _id: { 
      uuid: '$uuid' 
     }, 
     count: { 
      $sum: 1 
     }, 
     fps: { 
      $push: '$fp' 
     } 
    } 
    }, { 
    $project: { 
     'count': 1, 
     '_id': 0, 
     'uuid': '$_id', 
     'fps': 1 
    } 
    }, { 
    $match: { 
     count: { 
      $gt: 1 
     }, 
     $or: [{ 
      $and: [{ 
       fps: '100.100' 
      }, { 
       fps: '800000.800000' 
      }] 
     }, { 
      $and: [{ 
       fps: '350000.350000' 
      }, { 
       fps: '990000.990000' 
      }] 
     }, { 
      $and: [{ 
       fps: '330000.330000' 
      }, { 
       fps: '5000.5000' 
      }] 
     }, { 
      $and: [{ 
       fps: '705000.705000' 
      }, { 
       fps: '777000.777000' 
      }] 
     }, { 
      $and: [{ 
       fps: '200000.200000' 
      }, { 
       fps: '700.700' 
      }] 
     }] 
    } 
    }, { 
    $group: { 
     _id: { 
      uuid: '$count' 
     }, 
     count: { 
      $sum: 1 
     } 
    } 
    }, { 
    $project: { 
     'count': 1, 
     '_id': 0 
    } 
    }, ]); 

回答

0

你可以嘗試像下面。您可以包含所有條件值。

Aggregation agg = newAggregation(match(where("fp").in("100.100")), 
       group(fields().and("uuid", "uuid")).count().as("count").push("fp").as("fps"), 
       project(fields("count", "fps").and("uuid", "_id")).andExclude("_id"), 
       match(where("count").gt(1).orOperator(Criteria.where(null).andOperator(Criteria.where("fps").is("100.100"), Criteria.where("fps").is("800000.800000")))), 
       group(fields().and("uuid", "count")).count().as("count"), 
       project(fields("count")).andExclude("_id"));