我有一个嵌入式实体列表:
@Embedded
private List<EmbeddedEntity> embedded = new ArrayList<EmbeddedEntity>();
在这个列表中,我想搜索任何具有特定属性(我们称之为foo
)的嵌入式实体,而不是另一个(bar
)。所以在 Java 中foo
应该是非 null 和bar
null / 在 MongoDB 中不存在。
我尝试了以下代码(我确实有Entity
包含列表的 UUID):
Query<Entity> query = mongoDataStore.find(Entity.class).field("uuid").equal(uuid)
.field("embedded.foo").exists()
.field("embedded.bar").doesNotExist();
如果列表为空或只有一个条目(其中foo
有一个值bar
但尚不存在),这将正常工作。但是只要任何bar
属性有值,查询就会返回错误的结果。所以我正在寻找一个查询,它遍历所有嵌入的实体并触发任何缺失bar
的 . 那可能吗?
示例数据:
// the query does not pick up the entity as it doesn't have a foo -
// that's what I want
{ uuid: "...", [ { embedded: } ] }
// the query picks up the entity as there is a foo but no bar -
// that's what I want
{ uuid: "...", [ { embedded: { foo: date } } ] }
// the query does not pick up the entity - that's not what I want
// as one foo has a value and its bar doesn't
{ uuid: "...", [ { embedded: { foo: date, bar: date } },
{ embedded: { foo: date } }
] }
PS:我得到相同的结果.field("embedded.bar").hasThisOne(null)
。
PPS:手动遍历列表元素并不是一个真正的选择,因为我想使用查询进行更新操作。
PPS:我认为这是 Morphia 中的一个错误 - 请参阅下面的答案 ( https://stackoverflow.com/a/9705175/573153 ) 以了解解决方法