問題描述
Spring Data Mongo DB:回复消息長度5502322小於最大消息長度 (Spring Data Mongo DB: The reply message length 5502322 is less than the maximum message length)
代碼:
接口::
@Query(value = "{'status': {$in: ?0} , 'date':{ $lte: ?1 }}")
List<Blog> findByStatusAndCurrentDateWithOrdering(List<String> status, Date date, Sort order);
調用類:
findByStatusAndCurrentDateWithOrdering(status, new Date(), Sort.by(Direction.DESC, "date")
Azure中部署的應用程序出錯:
"status": 500,
"error": "Internal Server Error",
"message": "The reply message length 5502322 is less than the maximum message length 4194304; nested exception is com.mongodb.MongoIn
ternalException: The reply message length 5502322 is less than the maximum message length 4194304",
The MongoDB 中的內容包括 Base64 Image 以及 ID、HTML。(並且只有 11 或 12 行)
客戶的 Azure Mongo DB 版本是 <3.2.0。而在本地它是 > 3.2
上面的代碼可以做哪些改變來分塊讀取或限制它的工作?
參考解法
方法 1:
You need to change to this:
db.blog.aggregate([
{$match:{"status": {$in: status} , "date":{ $lte: date }}},
{$sort:{"date":‑1}}
],{allowDiskUse:true})
Edit:
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
...
//Inside your `@Service` class, include:
@Autowired
private MongoTemplate mongotemplate;
...
MatchOperation filter = match(Criteria.where("status").in(status).and("date").lte(date));
SortOperation sort = sort(Direction.DESC, "date")
Aggregation aggregation = newAggregation(filter, sort)
.withOptions(newAggregationOptions()
.allowDiskUse(true).build());
AggregationResults<Blog> result = mongoTemplate.aggregate(aggregation, mongoTemplate.getCollection(Blog.class), Blog.class);
List<Blog> blogs = result.getMappedResults();
(by fatherazrael、Valijon)