我正在尝试将使用 mongodb 驱动程序 3.0 的应用程序迁移到 java 17 中的 driver-synch 5.1。该应用程序具有接收以下格式的 json 的功能:
{"aggregate": "Vuce", "pipeline": [{"$match": {"Form": {"$eq": 109}}}, {"$project": {"Col1": "$DocInst", "Col2": "AAAAAAAAAAAA"}}], "cursor": {}}
将其作为“mongodb.runCommand(query)”执行,并返回聚合的结果(在本例中,我更愿意不更改查询文档的语法,因为它们有数百个,但我愿意接受建议).
但是,我在不同版本中有不同的文档,最终我无法访问较新文档中的命令结果。
在之前的版本中,聚合的结果在“结果”字段中有一个列表< Document >,其中包含所有符合条件的文档。这发生了变化,现在该命令返回具有以下格式的通用文档:
{cursor: Document{{firstBatch: [Document{{_id: 11376, Col2: AAAAAAAAAAAA}}, Document{{_id: 11568, Col2: AAAAAAAAAAAA}}, ...], id: 7067558295212936488, ns: Vuce.Vuce}}, ok: 1.0}}
生成的文档表明查询已解决,并包含包含“partial”结果的列表以及有关 MongoCursor 的信息。根据文档,想法是使用“getMore”来迭代集合。但是,由于它是一个 Document,所以包含的所有信息都是普通元素,因此(据我所知)无法获取 MongoCursor 对象以继续迭代。 我的 Maven 依赖项如下所示:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-core</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.21.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.21.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
我的问题是:如何迭代这个新版本的 runCommand?我可以从该结果中获取迭代器或游标吗?是否有另一种方法来执行返回迭代器的“查询”?
谢谢你
是的,你可以尝试这个方法。这是一个例子。
你的json:
{"aggregate": "Vuce", "pipeline": [{"$match": {"Form": {"$eq": 109}}}, {"$project": {"Col1": "$DocInst", "Col2": "AAAAAAAAAAAA"}}], "cursor": {}}
我有一个 json 的简化版本,它使用一个集合
test
和一个简单的
pipeline
。您提取集合名称和聚合管道并使用它。String s = """
{ "aggregate": "test", "pipeline": [{"$match": {"a": {"$eq": 1}}}], "cursor": {}}
""";
Bson json = new JsonObject(s);
BsonDocument bsonDoc = json.toBsonDocument();
List<BsonDocument> bsonPipe = bsonDoc.getArray("pipeline")
.stream()
.map(BsonValue::asDocument)
.toList();
String colName = bsonDoc.getString("aggregate").getValue();
// Assuming you have a MongoClient instance and a database called "test"
AggregateIterable<Document> aggResult = mongoClient.getDatabase("test")
.getCollection(colName)
.aggregate(bsonPipe);
List<Document> resultDocs = new ArrayList<>();
aggResult.into(resultDocs);
System.out.println(resultDocs);
服务器版本 4.4 和 Java 版本 17。我在我的
pom.xml
:
中使用了这个 Java 驱动程序依赖项
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>5.0.1</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>4.11.1</version>
</dependency>