Spring data-Mongo 数据库查询嵌入数组
Spring data-Mongo DB Query embedded array
我在 mongo 集合中有一个名为 (CustomerInformation) 的文档
具有以下结构。
{ "_id" : ObjectId("58f5e68c8205281d68bbb290"),
"_class" : "com.test.dataservices.entity.CustomerInformation",
"organizationInformation" : {
"_id" : "123",
"companyName" : "Test1",
"ibanNumber" : "12345e",
"address" : "estates",
"contractInformation" : {
"duration" : NumberInt(0),
"contractType" : "Gold",
"totalUsers" : NumberInt(0)
},
"users" : [
{
"firstName" : "testuser1",
"emailAddress" : "testuser1@test.com",
"password" : "test1@123",
"userAccessType" : "admin"
},
{
"firstName" : "testuser2",
"emailAddress" : "testuser2@test.com",
"password" : "test2@123",
"userAccessType" : "user"
}
]
}
}
现在我只想检索具有匹配的电子邮件地址和密码的用户信息。我正在尝试如下。
Criteria elementMatchCriteria = Criteria.where("organizationInformation.users").
elemMatch(Criteria.where("emailaddress").is("testuser1@test.com").and("password").is(test1@123));
BasicQuery query = new BasicQuery(elementMatchCriteria.getCriteriaObject());
CustomerInformation customer =mongoOperations.findOne(query, CustomerInformation.class);
我正在获取包含所有用户数组的完整文档,我只想检索匹配的用户信息 emailAddress 和密码。
我的查询或数据模型有什么问题?
有什么建议么?谢谢!
您可以使用 $unwind 的聚合查询来实现此目的
db.collection.aggregate([
{
$unwind:"$organizationInformation.users"
},
{
$match:{
"organizationInformation.users.emailAddress":"testuser1@test.com",
"organizationInformation.users.password":"test1@123"
}
},
{
$project:{
"organizationInformation.users":1
}
}
])
结果是:
{
"_id" : ObjectId("58f5e68c8205281d68bbb290"),
"organizationInformation" : {
"users" : {
"firstName" : "testuser1",
"emailAddress" : "testuser1@test.com",
"password" : "test1@123",
"userAccessType" : "admin"
}
}
}
或
db.collection.aggregate([
{
$unwind:"$organizationInformation.users"
},
{
$match:{
"organizationInformation.users.emailAddress":"testuser1@test.com",
"organizationInformation.users.password":"test1@123"
}
},
{
$project:{
user: "$organizationInformation.users"
}
}
])
结果是:
{
"_id" : ObjectId("58f5e68c8205281d68bbb290"),
"user" : {
"firstName" : "testuser1",
"emailAddress" : "testuser1@test.com",
"password" : "test1@123",
"userAccessType" : "admin"
}
}
使用位置投影。
Criteria elementMatchCriteria = Criteria.where("organizationInformation.users").elemMatch(Criteria.where("emailAddress").is("testuser1@test.com").and("password").is("test1@123"));
Query query = Query.query(elementMatchCriteria);
query.fields().position("organizationInformation.users", 1);
CustomerInformation customer =mongoOperations.findOne(query, CustomerInformation.class);
我在 mongo 集合中有一个名为 (CustomerInformation) 的文档 具有以下结构。
{ "_id" : ObjectId("58f5e68c8205281d68bbb290"),
"_class" : "com.test.dataservices.entity.CustomerInformation",
"organizationInformation" : {
"_id" : "123",
"companyName" : "Test1",
"ibanNumber" : "12345e",
"address" : "estates",
"contractInformation" : {
"duration" : NumberInt(0),
"contractType" : "Gold",
"totalUsers" : NumberInt(0)
},
"users" : [
{
"firstName" : "testuser1",
"emailAddress" : "testuser1@test.com",
"password" : "test1@123",
"userAccessType" : "admin"
},
{
"firstName" : "testuser2",
"emailAddress" : "testuser2@test.com",
"password" : "test2@123",
"userAccessType" : "user"
}
]
}
}
现在我只想检索具有匹配的电子邮件地址和密码的用户信息。我正在尝试如下。
Criteria elementMatchCriteria = Criteria.where("organizationInformation.users").
elemMatch(Criteria.where("emailaddress").is("testuser1@test.com").and("password").is(test1@123));
BasicQuery query = new BasicQuery(elementMatchCriteria.getCriteriaObject());
CustomerInformation customer =mongoOperations.findOne(query, CustomerInformation.class);
我正在获取包含所有用户数组的完整文档,我只想检索匹配的用户信息 emailAddress 和密码。 我的查询或数据模型有什么问题? 有什么建议么?谢谢!
您可以使用 $unwind 的聚合查询来实现此目的
db.collection.aggregate([
{
$unwind:"$organizationInformation.users"
},
{
$match:{
"organizationInformation.users.emailAddress":"testuser1@test.com",
"organizationInformation.users.password":"test1@123"
}
},
{
$project:{
"organizationInformation.users":1
}
}
])
结果是:
{
"_id" : ObjectId("58f5e68c8205281d68bbb290"),
"organizationInformation" : {
"users" : {
"firstName" : "testuser1",
"emailAddress" : "testuser1@test.com",
"password" : "test1@123",
"userAccessType" : "admin"
}
}
}
或
db.collection.aggregate([
{
$unwind:"$organizationInformation.users"
},
{
$match:{
"organizationInformation.users.emailAddress":"testuser1@test.com",
"organizationInformation.users.password":"test1@123"
}
},
{
$project:{
user: "$organizationInformation.users"
}
}
])
结果是:
{
"_id" : ObjectId("58f5e68c8205281d68bbb290"),
"user" : {
"firstName" : "testuser1",
"emailAddress" : "testuser1@test.com",
"password" : "test1@123",
"userAccessType" : "admin"
}
}
使用位置投影。
Criteria elementMatchCriteria = Criteria.where("organizationInformation.users").elemMatch(Criteria.where("emailAddress").is("testuser1@test.com").and("password").is("test1@123"));
Query query = Query.query(elementMatchCriteria);
query.fields().position("organizationInformation.users", 1);
CustomerInformation customer =mongoOperations.findOne(query, CustomerInformation.class);