程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何从 mongo DB 集合中获取所有数据并将它们返回到带有 morphia 的 ArrayList 中大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何从 mongo DB 集合中获取所有数据并将它们返回到带有 morphia 的 ArrayList 中?

开发过程中遇到如何从 mongo DB 集合中获取所有数据并将它们返回到带有 morphia 的 ArrayList 中的问题如何解决?下面主要结合日常开发的经验,给出你关于如何从 mongo DB 集合中获取所有数据并将它们返回到带有 morphia 的 ArrayList 中的解决方法建议,希望对你解决如何从 mongo DB 集合中获取所有数据并将它们返回到带有 morphia 的 ArrayList 中有所启发或帮助;

我试图找到如何使用 Morphia https://morphia.dev/morphia/2.1/querying.html 获取 mongo DB 集合中的所有数据并将它们返回到 ArrayList 中(就像您从基本的 get API 调用中接收数据一样)

最初我没有 mongo 数据库,我从内存数据库中返回所有数据,如下所示:

final Map<String,Product> productsByID = new HashMap<>();

@OverrIDe
public List<Product> findAll() {
    return new ArrayList<>(productsByID.values());
}

@OverrIDe
public Product findByID(ProductID productID) {
    if (!productsByID.containsKey(productID.toString())) {
        throw new ProductNotFoundException();
    }
    return productsByID.get(productID.toString());
}

现在我正在尝试使用 Morphia 和 Mongo 数据库来实现相同的原理,但我正在努力寻找如何做到这一点。

这是我的尝试,但没有成功:

final Map<String,ProductMongoAssembler> productsByID = new HashMap<>();

    @OverrIDe
    public List<Product> findAll() {
        query<ProductDtoMongo> query = datastore.find(ProductDtoMongo.class).filter();
        query.forEach(i -> productsByID.put(i.productID.toString(),productMongoAssembler));

        return new ArrayList(productsByID.values());
    }

当我去检查我的 API 调用中发生了什么时,我看到“类没有字段”

如何从 mongo DB 集合中获取所有数据并将它们返回到带有 morphia 的 ArrayList 中

我只想从我的集合中收集所有数据并返回一个包含数据的 ArrayList。

感谢您的帮助:)

解决方法

试试datastore.find(ProductDtoMongo.class).iterator().toList()? (我需要在那里修复一个文档错误。@H_48_7@morphiacursor 不应该在内部包中。它是一个带有 @H_48_7@mongocursor 辅助方法的 toList()。)

将它们全部倾倒到 @H_48_7@map 以获取 values() 似乎非常浪费。

大佬总结

以上是大佬教程为你收集整理的如何从 mongo DB 集合中获取所有数据并将它们返回到带有 morphia 的 ArrayList 中全部内容,希望文章能够帮你解决如何从 mongo DB 集合中获取所有数据并将它们返回到带有 morphia 的 ArrayList 中所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。