我在 mongodb 集合“Contact”中有以下文档结构。有一个名为“numbers”的子文档数组:
{
"name" : "Bill",
"numbers" : [
{
"type" : "home",
"number" : "01234",
},
{
"type" : "business",
"number" : "99099"
},
{
"type" : "fax",
"number" : "77777"
}
]
}
当我只想查询“家庭”和“企业”号码时,我可以在 mongodb-shell 中执行以下操作:
db.Contact.find({ numbers: { $elemMatch: {
type : { $in : ["home", "business"]},
number: { $regex : "^012" }
}}});
但是如何在吗啡中做到这一点呢?有什么办法吗?
我了解吗啡支持“$elemMatch”。所以我可以做这样的事情:
query.filter("numbers elem", ???);
但是我到底如何为子文档添加组合查询呢?
最佳答案
为时已晚,但也许其他人可以方便地找到它。
我找到了解决方案 https://groups.google.com/forum/#!topic/morphia/FlEjBoSqkhg
query.filter("numbers elem",
BasicDBObjectBuilder.start()
.push("type").add("$in", new String[]{"home", "business"}).pop()
.push("number").add("$regex", "^012").pop().get());
关于java - 使用 elem 对子文档数组进行 Morphia 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18510593/