org.bson.BsonSerializationException: Maximum serialization depth exceeded (does the object being serialized have a circular reference?).
at org.bson.AbstractBsonWriter.writeStartDocument(AbstractBsonWriter.java:283)
at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:190)
at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:182)
at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:199)
at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:182)
at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:199)
at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:182)
at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:199)
at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:182)
at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:199)
at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:182)
at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:199)
at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:182)
at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:199)
at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:182)
at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:199)
at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:182)
at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:199)
at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:182)
at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:199)
at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:182)
at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:199)
at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:182)
at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:199)
在Java连接MongoDB对文档进行操作时出现了以上的错误异常,报的是什么BSON序列化异常,仔细排查发现是由于文档嵌套那一块错了
/***
* 查询文档中所有size的总和
*/
@Test
public void selectDocumentAggregateSum(){
MongoCollection collection = MongoAuthPoolUtil.getCollection("develop", "dev");
List<Document> list = new ArrayList<>();
Document sum = new Document();
sum.append("$sum","$size");
Document sizeAll = new Document();
sizeAll.append("_id",null).append("sizeAll",sum);
Document document = new Document();
document.append("$group",sizeAll);
list.add(document);
AggregateIterable iterable = collection.aggregate(list);
MongoCursor cursor = iterable.iterator();
while(cursor.hasNext()){
Document docu = (Document) cursor.next();
System.out.println(docu.get("sizeAll"));
}
}