Morphia嵌入链接对象列表搜索条件

时间:2012-07-18 16:13:06

标签: java mongodb search morphia

我正在使用MongoDB和java-lib Morphia来处理它。

我有两个班级:

class User {
   ...

   #Reference
   private List<Invoice> invoiceTransactions = new ArrayList<Invoice>();

   ...
}

class Invoice {
   ...

   User user;

   ...
}

当我创建发票时,我将其放入 invoiceTransactions 列表

现在我有一个发票对象,我的目标是在 invoiceTransactions 列表中找不到包含此发票的相应用户。< / p>

这就是我写的:

Invoice invoice = ...;

Query<User> query = (Query) dao.createQuery();
query.disableValidation().filter(Mapper.ID_KEY, u.getId());
List<Invoice> inv = new ArrayList<Invoice>();
inv.add(invoice);
query.criteria(User.INVOICE_TRANSACTIONS).hasNoneOf(inv);

这是MongoDB日志:

Wed Jul 18 19:03:09 [conn48] update dwh.UserImpl query: { _id: ObjectId('4ffbc8d
943b693ac766255f3'), InvoiceTransactions: { $nin: [ { className: "abc.InvoiceImpl", sum: 70.0, invoiceStatus: "NEW", creationDate:
new Date(1342627389037), _id: ObjectId('5006de3d022993ac397609c4'), guid: "4DEAB
CCE-63F9-1B0A-59F2-1135A83054B9", day: "2012.07.18", hour: "2012.07.18 19", minu
te: "2012.07.18 19:03" } ] } } update: { $set: { authId: "bla-bla" } } 0ms

您可以看到 Invoice 对象的处理方式与引用不同,但与嵌入式对象相同。因此,即使实际上 invoiceTransactions 包含发票,我也会触发该条件。

所以,我的问题是如何强制morphia查看 inv 对象,而不是将其放入带有所有字段的结果查询中?

1 个答案:

答案 0 :(得分:0)

Okey,这是答案:

Key<Invoice> invoiceKey = dao.getDatastore().getKey(invoice);
List<Key<Invoice>> invoiceKeyList = new ArrayList<Key<Invoice>>();
invoiceKeyList.add(invoiceKey);
query.criteria(User.INVOICE_TRANSACTIONS).hasNoneOf(invoiceKeyList);