gpt4 book ai didi

java - 异常来自 Collection 的不同值 - CurrentBSONType 是 UNDEFINED

转载 作者:搜寻专家 更新时间:2023-10-30 20:53:13 24 4
gpt4 key购买 nike

我在进行从 Mongo 集合中获取不同记录的数据库调用时收到“CurrentBSONType is UNDEFINED”异常。

我们的 Mongo 集合看起来像这样:

    {
"_id": ObjectId("5672be450d71c945772b8393"),
"productId": "P-moac-49678195574",
"title": "Flip Cover Lenovo",
"description": "Cosmos Flip Cover ",
"currency": "INR",
"productURL": "",
"sellingPrice": 150,
"subcategories": [{
"name": "Mobile Accessories",
"parent": {
"name": "Mobile Accessories"
}
}],
"imageURLs": [{
"imageurl": "",
"height": 0,
"width": 0
}],
"attributes": [{
"key": "model",
"value": "Flip Cover"
}],
"created": ISODate("2015-12-17T13:53:09.757Z"),
"modified": ISODate("2015-12-17T13:53:09.757Z"),
"merchant": "IB",
"brand": "Cosmos",
"reviews": null
}

Java 代码获取子类别的distinct 值:

    public Map<String, SubCategory> getSubCategories() throws StatementCreationException, SQLException {
MongoCollection<Document> productsCollection = connectionFactory.getMongoCollection("products");
DistinctIterable<Document> result = productsCollection.distinct("subcategories", Document.class);
Map<String, SubCategory> subcategories = new LinkedHashMap<String, SubCategory>();
for (Iterator<Document> iterator = result.iterator(); iterator.hasNext();) {
Document subCatDoc = iterator.next();
String subCatName = subCatDoc.getString("name");
if (subCatName == null || subCatName.trim().isEmpty()) {
continue;
}
SubCategory subCat = new SubCategoryImpl();
subCat.setName(subCatName);
Document categoryDoc = (Document)subCatDoc.get("parent");
if (categoryDoc != null) {
Category category = new CategoryImpl();
category.setName(categoryDoc.getString("name"));
subCat.setCategory(category);
}
subcategories.put(subCatName, subCat);
}
return subcategories;
}

当调用上述方法时,MongoDb 抛出以下异常:

Exception in thread "main" org.bson.BsonInvalidOperationException: readStartDocument can only be called when CurrentBSONType is DOCUMENT, not when CurrentBSONType is UNDEFINED.

完整的堆栈跟踪:

Exception in thread "main" org.bson.BsonInvalidOperationException: readStartDocument can only be called when CurrentBSONType is DOCUMENT, not when CurrentBSONType is UNDEFINED.
at org.bson.AbstractBsonReader.verifyBSONType(AbstractBsonReader.java:655)
at org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:687)
at org.bson.AbstractBsonReader.readStartDocument(AbstractBsonReader.java:421)
at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:138)
at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:45)
at com.mongodb.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:48)
at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:53)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:81)
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:40)
at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:122)
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:260)
at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:147)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:166)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:157)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:82)
at com.mongodb.operation.DistinctOperation$1.call(DistinctOperation.java:129)
at com.mongodb.operation.DistinctOperation$1.call(DistinctOperation.java:126)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:196)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:169)
at com.mongodb.operation.DistinctOperation.execute(DistinctOperation.java:126)
at com.mongodb.operation.DistinctOperation.execute(DistinctOperation.java:56)
at com.mongodb.Mongo.execute(Mongo.java:760)
at com.mongodb.Mongo$2.execute(Mongo.java:747)
at com.mongodb.OperationIterable.iterator(OperationIterable.java:47)
at com.mongodb.DistinctIterableImpl.iterator(DistinctIterableImpl.java:80)
at com.mr10q.parser.dao.service.mongo.impl.MongoDBPersistentServiceImpl.getSubCategories(MongoDBPersistentServiceImpl.java:713)

最佳答案

我认为唯一的问题是“子类别”是一个文档数组。

因此,将 Document[].class 作为 distinct 调用的 resultClass 参数进行传递将执行您想要的操作。

例如:

DistinctIterable<Document[]> result = productsCollection.distinct("subcategories", Document[].class);

关于java - 异常来自 Collection 的不同值 - CurrentBSONType 是 UNDEFINED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34556689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com