「範囲選択」が正しく指定されておらず、 の使用法が$and
正しくありません。実際には、「最後の」引数のみが考慮されているため、 「等しい日付よりも大きい」myId
10
すべてを探しているだけですが、これはもちろん正しくありません。
の正しいクエリ構文は次のとおりです$match
。
{ "$match": {
"myDate": {
"$gte": new Date(949384052490),
"$lte": new Date(1448257684431)
},
"myId": 10,
"type": { "$ne": "Contractor" }
}}
$and
とにかく、すべての MongoDB クエリ引数は既にAND条件であるため、必要はありません。
$project
また、との段階を組み合わせることも検討する必要があります。$group
これは通常、これらが次々に発生したときに組み合わせることができることを意味します。少なくともその方が効率的です。
しかしもちろん、初期段階で多くの時間が浪費され$match
ており、とにかく誤った結果を選択していたはずです。
$group
および no の最適なパイプライン$project
:
{ "$group": {
"_id": {
"retailerName": "$retailerName",
"year": { "$year": "$myDate" },
"currency": "$currency"
},
"netSales": { "$sum": "$revenue" },
"netUnitSold": { "$sum": "$unitSold" },
"totalSales": { "$sum":
{ "$multiply": [ "$unitSold", "$itemPrice" ] }
}
}}
したがって、パイプライン全体はちょうど$match
then$group
です。
春のmongoでの作業
spring-mongo を使用している場合、複合キーとアキュムレータの計算値を使用した、サポートされている演算子による結合には現在の制限があり$group
ますが、これらを回避できます。ステートメントに関して$and
は、それは実際には構文の問題であり、Spring mongo のせいではありません。
まず、集計パイプラインで「グループ」のカスタム クラスを設定します。
public class CustomGroupOperation implements AggregationOperation {
private DBObject operation;
public CustomGroupOperation (DBObject operation) {
this.operation = operation;
}
@Override
public DBObject toDBObject(AggregationOperationContext context) {
return context.getMappedObject(operation);
}
}
次に、そのクラスを使用してパイプラインを構築します。
Aggregation aggregation = newAggregation(
match(
Criteria.where("myDate")
.gte(new Date(new Long("949384052490")))
.lte(new Date(new Long("1448257684431")))
.and("myId").is(10)
.and("type").ne("Contractor")
),
new CustomGroupOperation(
new BasicDBObject(
"$group", new BasicDBObject(
"_id", new BasicDBObject(
"retailerName", "$retailerName"
).append(
"year", new BasicDBObject("$year", "$myDate")
).append(
"currency", "$currency"
)
).append(
"netSales", new BasicDBObject("$sum","$revenue")
).append(
"netUnitSold", new BasicDBObject("$sum","$unitSold")
).append(
"totalSales", new BasicDBObject(
"$multiply", Arrays.asList("$unitSold", "$itemPrice")
)
)
)
)
);
これにより、次のようなシリアル化されたパイプラインが生成されます。
[
{ "$match" : {
"myDate" : {
"$gte" : { "$date" : "2000-02-01T05:47:32.490Z"},
"$lte" : { "$date" : "2015-11-23T05:48:04.431Z"}
},
"myId" : 10,
"type" : { "$ne" : "Contractor"}
}},
{ "$group": {
"_id" : {
"retailerName" : "$retailerName",
"year" : { "$year" : "$myDate"},
"currency" : "$currency"
},
"netSales" : { "$sum" : "$revenue"},
"netUnitSold" : { "$sum" : "$unitSold"},
"totalSales" : { "$multiply" : [ "$unitSold" , "$itemPrice"]}
}}
]
これは、上記の例とまったく同じです