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

时间:2016-01-01 13:52:02

标签: java mongodb database nosql

我得到" CurrentBSONType未完成"在进行从Mongo集合中获取不同记录的db调用时出现异常。

我们的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代码获取子类别的不同值:

    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)

1 个答案:

答案 0 :(得分:0)

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

因此传递 Document [] .class 作为distinct调用的resultClass参数可以做你想要的。

E.g:

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