Код: Выделить всё
{
"_id": {"$oid": "65ea0c9d638cd57f51c22cb7"},
"listing": {
"listingId": 709834373,
"listDate": {"$date": "2023-10-12T00:00:00.000Z"},
"price": {
"currencyCode": "USD",
"value": 1000
},
}
}
Код: Выделить всё
public Optional searchResultsSummaryData(SearchFilter searchFilter) {
SearchOperation searchOperation = buildSearchOperationFromFilter(searchFilter);
SearchAggregation searchAggregation = SearchAggregation.builder()
.searchOperation(searchOperation)
.build();
Document aggregationDoc = new Document();
mongoConverter.write(searchAggregation, aggregationDoc);
GroupOperation groupOperation = group("listing.price.value").avg("$avg(listing.price.value)").as("avgPrice");
Document groupDoc = new Document();
mongoConverter.write(groupOperation, groupDoc);
log.debug("groupDoc: {}", groupDoc.toJson());
Document groupStage = new Document("$group", groupDoc);
Document projectDocDetail = new Document("avgPrice", 1);
Document projectDoc = new Document("$project", projectDocDetail);
MongoCollection collection = mongoOperations.getCollection(LISTING_DOCUMENT.collection());
List aggList = List.of(aggregationDoc, groupStage, projectDoc);
log.debug("aggList: {}", toJson(aggList));
AggregateIterable aggregationResult = collection.aggregate(aggList);
Document summaryDocument = aggregationResult.first();
if (summaryDocument == null) {
return Optional.empty();
} else {
return Optional.of(defaultMongoConverter.read(SummaryData.class, summaryDocument));
}
}
Код: Выделить всё
com.mongodb.MongoCommandException: Command failed with error 40234 (Location40234): 'The field 'idFields' must be an accumulator object' on server pl-1-us-west-2.c8foo.mongodb.net:1037. The full response is {"ok": 0.0, "errmsg": "The field 'idFields' must be an accumulator object", "code": 40234, "codeName": "Location40234", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1719546021, "i": 8}}, "signature": {"hash": {"$binary": {"base64": "rOfPFKpgglg5DigfTjeL2n1qgeg=", "subType": "00"}}, "keyId": 7329363855287517191}}, "operationTime": {"$timestamp": {"t": 1719546021, "i": 8}}}
at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:205) ~[mongodb-driver-core-4.11.2.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:454) ~[mongodb-driver-core-4.11.2.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:372) ~[mongodb-driver-core-4.11.2.jar:na]
at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:114) ~[mongodb-driver-core-4.11.2.jar:na]
at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:765) ~[mongodb-driver-core-4.11.2.jar:na]
Код: Выделить всё
[
{
"$search": {
...
}
},
{
"$group": {
"idFields": {
"originalFields": [
{
"synthetic": false,
"field": {
"raw": "listing.price.value",
"name": "price.value",
"target": "listing.price.value"
}
}
],
"syntheticFields": []
},
"operations": [
{
"op": "AVG",
"key": "avgPrice",
"reference": "$avg(listing.price.value)"
}
]
}
},
{
"$project": {
"avgPrice": 1
}
}
]
Код: Выделить всё
$group: {
_id: null,
avgPrice: { $avg: "$listing.price.value" }
}
Я пробовал передавать различные имена полей в метод group(), но в основном я получите ту же ошибку.
Будем очень благодарны за любую помощь.
Подробнее здесь: https://stackoverflow.com/questions/786 ... tor-object
Мобильная версия