通过Id查询Morphia

发布于 2024-10-07 22:29:45 字数 157 浏览 7 评论 0原文

我正在使用 Morphia(MongoDB 的 Pojo 映射器),我发现一项在我看来应该非常简单的任务很困难:通过 id 获取对象。我能够找到集合中的所有对象,但无法弄清楚使用从列表中获得的 id 进行查询的简单任务。我实际上是在谈论 ObjectId。如果我尝试以 JSON 格式呈现它,我会看到

I am using Morphia, the Pojo mapper for MongoDB, and I find difficult a task that in my view should be very simple: getting an object by id. I am able to find all the objects in a collection but I cannot figure out the simple task of querying using an id I got from the list. I am actually talking about the ObjectId. If I try to render it in JSON I see

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空心空情空意 2024-10-14 22:29:46

前面的解释使用了已弃用的方法:datastore().find(Model.class).field("_id").equal(oid).get(); 我在 Morphia 2.2 上的变体。 10:

public Optional<Hotel> findById(final String id) {
    if (!ObjectId.isValid(id)) {
        throw new SomeException();
    }

    final Query<Hotel> query = datastore.find(Hotel.class)
        .filter(eq("_id", new ObjectId(id)));
    return Optional.ofNullable(query.first());
}

The previous explanation used the deprecated methods: datastore().find(Model.class).field("_id").equal(oid).get(); My variant of it on Morphia 2.2.10:

public Optional<Hotel> findById(final String id) {
    if (!ObjectId.isValid(id)) {
        throw new SomeException();
    }

    final Query<Hotel> query = datastore.find(Hotel.class)
        .filter(eq("_id", new ObjectId(id)));
    return Optional.ofNullable(query.first());
}
可遇━不可求 2024-10-14 22:29:45

这个问题似乎不完整。

您的问题的答案似乎也位于 Morphia 快速入门页面上。看起来很简单,如下所示。

Datastore ds = morphia.createDatastore("testDB");
String hotelId = ...; // the ID of the hotel we want to load
// and then map it to our Hotel object
Hotel hotel = ds.get(Hotel.class, hotelId);

所以你肯定需要更多细节。

This question seems incomplete.

It also seems like the answer to you question is on the Morphia QuickStart page. Seems to be as simple as follows.

Datastore ds = morphia.createDatastore("testDB");
String hotelId = ...; // the ID of the hotel we want to load
// and then map it to our Hotel object
Hotel hotel = ds.get(Hotel.class, hotelId);

So you'll definitely need more details.

回梦 2024-10-14 22:29:45
Datastore ds = morphia.createDatastore("testDB");
String hotelId = "516d41150364a6a6697136c0"; // the ID of the hotel we want to load
ObjectId objectId = new ObjectId(hotelId);
// and then map it to our Hotel object
Hotel hotel = ds.get(Hotel.class, objectId);
Datastore ds = morphia.createDatastore("testDB");
String hotelId = "516d41150364a6a6697136c0"; // the ID of the hotel we want to load
ObjectId objectId = new ObjectId(hotelId);
// and then map it to our Hotel object
Hotel hotel = ds.get(Hotel.class, objectId);
默嘫て 2024-10-14 22:29:45

如果您通过 id 查找,并且 id 是由用户提供的(意味着它可以是任何类型的数据),则不应使用上面给出的解决方案。

文档,一个 ObjectId 由 12 个字节组成,因此如果您将其他内容传递给 new ObjectId(myValue),您的代码将抛出 IllegalArgumentException

这是我实现按 id 查找的方法的方法:

public Model findById(String id) throws NotFoundException {
    if (!ObjectId.isValid(id)) {
        throw new NotFoundException();
    }

    ObjectId oid = new ObjectId(id);
    Model m = datastore().find(Model.class).field("_id").equal(oid).get();
    if (m == null) {
        throw new NotFoundException();
    }
    return m;
}

If you're finding by id and the id is provided by the user (means that it could be whatever type of data), you shouldn't use the solutions given above.

As explained in the documentation, an ObjectId consists of 12 bytes, so if you pass something else to new ObjectId(myValue), your code will throw an IllegalArgumentException.

Here is how I implemented the method to find by id :

public Model findById(String id) throws NotFoundException {
    if (!ObjectId.isValid(id)) {
        throw new NotFoundException();
    }

    ObjectId oid = new ObjectId(id);
    Model m = datastore().find(Model.class).field("_id").equal(oid).get();
    if (m == null) {
        throw new NotFoundException();
    }
    return m;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文