【问题标题】:Morphia Query by Reference ObjectIDMorphia 按引用 ObjectID 查询
【发布时间】:2014-11-19 07:11:15
【问题描述】:

这是我的实体的定义:

@Entity("Comment")
public class Comment extends BaseEntity {

    @Reference
    private Merchant merchant;

    ...
}

@Entity("Merchant")
class Merchant extends BaseEntity{
    @Id
    @Property("id")
    protected ObjectId id;

    ...
}

这是我的数据:

comment:{
"_id": ObjectId("546c1ac64652e5180dc21577"),
"merchant" : DBRef("Merchant", ObjectId("546c1ac64652e5180dc21576")),

...
}

当我创建如下查询时:

Query<Comment> query = ds.createQuery(Comment.class);
query.field("merchant").equal(new ObjectId("546c1ac64652e5180dc21576"));

commentDao.findOne(query);

没有返回结果,请问用商家的ObjectId查询评论数据的正确方法是什么?

感谢您的帮助。

【问题讨论】:

    标签: mongodb reference morphia


    【解决方案1】:
    Query<Comment> query = ds.find(Comment.class).disableValidation()
        .field("Merchant").equal(new Key<>(Merchant.class, merchantId);
    

    我认为你需要禁用验证,否则你会看到一些相当不必要的警告。

    您可以直接查询 DBRef ID,但由于 DBRef 本身是键入的,除非您有正当理由,否则我不会绕过它。

    【讨论】:

    • 我刚刚添加了函数disableValidation(),但还是不行。没有返回结果。我必须查询 Merchant 对象,然后通过这个 Merchant 对象查询 Comment。
    • 1) 应该是 "Merchant" — 恕我直言,将集合大写并不常见。 2) merchantId 包含正确的 ID 并且是 ObjectId?
    【解决方案2】:

    我不喜欢 Morphia 使用 DBRef 的方式,因为它可以轻松地只使用 ObjectId(但 DBRef 确实包含允许您对 Merchant 进行子类化的类名)。

    无论如何,你应该可以做到:

    Query<Comment> query = ds.createQuery(Comment.class);
    query.field("merchant.$id").equal(new ObjectId("546c1ac64652e5180dc21576")
    

    或使用纯 Java 驱动程序

    collection.find(new BasicDBObject("merchant",
        new BasicDBObject("$ref", "Merchant")
          .append("$id", new ObjectId("546c1ac64652e5180dc21576"))))
    

    【讨论】:

      【解决方案3】:

      正确的方法是获取带有ID的Merchant对象,然后传递给Comment查询:

      Query<Merchant> merchantQuery = ds.createQuery(Merchant.class);
      merchantQuery.field("id").equal(new ObjectId(merchantId)); 
      Merchant merchant = merchantQuery.get();     // Get the Merchant object
      
      Query<Comment> commentQuery = ds.createQuery(Comment.class);
      commentQuery.filter("merchant", merchant);
      commentQuery.get()                           // Get the Comment object
      

      【讨论】:

        【解决方案4】:

        你可以用mongodb驱动来做

                MongoClient mongo =new MongoClient(mongoURI);
                DBCollection dbCollection =mongo.getDB("<your_db>").getCollection("Comment");
                DBObject dbObject = (DBObject) JSON.parse("{ 'merchant' : { '$ref' : 'Merchant' , '$id' : { '$oid' : '5693e72244ae9fe4803a4520'}}}");
        
                String json=JSON.serialize(dbCollection.find(dbObject));
        

        注意 json 中的单个 qoute。

        在 Morphia 中像:

        Query q = MorphiaObject.datastore.find(Comment.class).field("merchant").equal(MorphiaObject.datastore.get(Merchant.class,new ObjectId("5693e72244ae9fe4803a4520")));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-07-22
          • 1970-01-01
          • 2014-12-13
          • 1970-01-01
          • 2012-04-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多