Arrays 如何使用morphia过滤mongo文档中的嵌入数组
鉴于我的配置文件数据如下所示,我希望找到用户名和productId组合的配置文件 并且仅返回配置文件以及该产品的相应合同Arrays 如何使用morphia过滤mongo文档中的嵌入数组,arrays,mongodb,morphia,Arrays,Mongodb,Morphia,鉴于我的配置文件数据如下所示,我希望找到用户名和productId组合的配置文件 并且仅返回配置文件以及该产品的相应合同 { "firstName": "John", "lastName": "Doe", "userName": "john.doe@gmail.com", "language": "NL", "timeZone": "Europe/Amsterdam", "contracts": [ { "c
{
"firstName": "John",
"lastName": "Doe",
"userName": "john.doe@gmail.com",
"language": "NL",
"timeZone": "Europe/Amsterdam",
"contracts": [
{
"contractId": "DEMO1-CONTRACT",
"productId": "ticket-api",
"startDate": ISODate('2016-06-29T09:06:42.391Z'),
"roles": [
{
"name": "Manager",
"permissions": [
{
"activity": "ticket",
"permission": "createTicket"
},
{
"activity": "ticket",
"permission": "updateTicket"
},
{
"activity": "ticket",
"permission": "closeTicket"
}
]
}
]
},
{
"contractId": "DEMO2-CONTRACT",
"productId": "comment-api",
"startDate": ISODate('2016-06-29T10:27:45.899Z'),
"roles": [
{
"name": "Manager",
"permissions": [
{
"activity": "comment",
"permission": "createComment"
},
{
"activity": "comment",
"permission": "updateComment"
},
{
"activity": "comment",
"permission": "deleteComment"
}
]
}
]
}
]
}
我设法从命令行找到了解决方案。但我似乎找不到一种方法来实现这一点与莫菲亚(最新版本)
这就是我目前所拥有的。非常感谢您的帮助
Query<Profile> matchQuery = getDatastore().createQuery(Profile.class).field(Profile._userName).equal(userName);
getDatastore()
.createAggregation(Profile.class)
.match(matchQuery)
.project(Projection.expression(??))
Query matchQuery=getDatastore().createQuery(Profile.class).field(Profile.\u userName).equal(userName);
getDatastore()
.createAggregation(Profile.class)
.match(匹配查询)
.project(投影表达式(??)
注意。。。同时,我发现了另一个不使用聚合管道的解决方案
public Optional<Profile> findByUserNameAndContractQuery(String userName, String productId) {
DBObject contractQuery = BasicDBObjectBuilder.start(Contract._productId, productId).get();
Query<Profile> query =
getDatastore()
.createQuery(Profile.class)
.field(Profile._userName).equal(userName)
.filter(Profile._contracts + " elem", contractQuery)
.retrievedFields(true, Profile._contracts + ".$");
return Optional.ofNullable(query.get());
}
Query<Profile> matchQuery = getDatastore().createQuery(Profile.class).field(Profile._userName).equal(userName);
getDatastore()
.createAggregation(Profile.class)
.match(matchQuery)
.project(Projection.expression("$filter", new BasicDBObject()
.append("input", "$contracts")
.append("as", "contract")
.append("cond", new BasicDBObject()
.append("$eq", Arrays.asList('$$contract.productId', "ticket-api")));
public可选findByUserNameAndContractQuery(字符串用户名,字符串productId){
DBObject contractQuery=BasicDBObjectBuilder.start(Contract.\u productId,productId).get();
查询=
getDatastore()
.createQuery(Profile.class)
.field(Profile.\u用户名).equal(用户名)
.filter(Profile._contracts+“elem”,contractQuery)
.retrievedFields(true,Profile._contracts+“$”);
返回可选的.ofNullable(query.get());
}
我终于找到了过滤嵌入数组的最佳方法(假设我只想从数组返回最多1个元素)
db.Profile.aggregate([
{ $match: {"userName": "john.doe@gmail.com"}},
{ $unwind: "$contracts"},
{ $match: {"contracts.productId": "comment-api"}}
])
要根据您的第一个设计进行匹配,您可以使用morphia聚合管道尝试投影设置
public Optional<Profile> findByUserNameAndContractQuery(String userName, String productId) {
DBObject contractQuery = BasicDBObjectBuilder.start(Contract._productId, productId).get();
Query<Profile> query =
getDatastore()
.createQuery(Profile.class)
.field(Profile._userName).equal(userName)
.filter(Profile._contracts + " elem", contractQuery)
.retrievedFields(true, Profile._contracts + ".$");
return Optional.ofNullable(query.get());
}
Query<Profile> matchQuery = getDatastore().createQuery(Profile.class).field(Profile._userName).equal(userName);
getDatastore()
.createAggregation(Profile.class)
.match(matchQuery)
.project(Projection.expression("$filter", new BasicDBObject()
.append("input", "$contracts")
.append("as", "contract")
.append("cond", new BasicDBObject()
.append("$eq", Arrays.asList('$$contract.productId', "ticket-api")));
Query matchQuery=getDatastore().createQuery(Profile.class).field(Profile.\u userName).equal(userName);
getDatastore()
.createAggregation(Profile.class)
.match(匹配查询)
.project(Projection.expression(“$filter”,新的BasicDBObject())
.append(“输入”和“$contracts”)
.附加(“作为”、“合同”)
.append(“cond”,新的基本对象()
.append(“$eq”,Arrays.asList(“$$contract.productId”,“ticketapi”));
另请参见morphia工作人员在第88行附近编写的示例