2017-04-18 11 views
1

以下の構造の(CustomerInformation) というmongoコレクションのドキュメントを持っています。春データ-Mongo DBクエリ埋め込み配列

 {  "_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" : "[email protected]", 
         "password" : "[email protected]", 
         "userAccessType" : "admin" 
        }, 
        { 

         "firstName" : "testuser2", 
         "emailAddress" : "[email protected]", 
         "password" : "[email protected]", 
         "userAccessType" : "user" 
        } 
       ] 
      } 
     } 

これで、emailAddressとPasswordが一致するユーザー情報のみを取得します。私は次のように努力しています。

Criteria elementMatchCriteria = Criteria.where("organizationInformation.users"). 
    elemMatch(Criteria.where("emailaddress").is("[email protected]").and("password").is([email protected])); 

    BasicQuery query = new BasicQuery(elementMatchCriteria.getCriteriaObject()); 

    CustomerInformation customer =mongoOperations.findOne(query, CustomerInformation.class); 

私は、すべてのユーザーの配列と完全な文書を取得しています、私は唯一の一致するユーザー情報EMAILADDRESSとパスワードを取得したいです。 クエリまたはデータモデルで何が間違っていますか? 提案がありますか?ありがとうございました!

答えて

0

位置投影を使用します。

Criteria elementMatchCriteria = Criteria.where("organizationInformation.users").elemMatch(Criteria.where("emailAddress").is("[email protected]").and("password").is("[email protected]")); 
Query query = Query.query(elementMatchCriteria); 
query.fields().position("organizationInformation.users", 1); 
CustomerInformation customer =mongoOperations.findOne(query, CustomerInformation.class); 
+0

ありがとうございます。その仕事.. –

0

$この

db.collection.aggregate([ 
    { 
     $unwind:"$organizationInformation.users" 
    }, 
    { 
     $match:{ 
      "organizationInformation.users.emailAddress":"[email protected]", 
      "organizationInformation.users.password":"[email protected]" 
     } 
    }, 
    { 
     $project:{ 
      "organizationInformation.users":1 
     } 
    } 
]) 

結果が達成するためにほどくとあなたが集計クエリを使用することができます。

{ 
    "_id" : ObjectId("58f5e68c8205281d68bbb290"), 
    "organizationInformation" : { 
     "users" : { 
      "firstName" : "testuser1", 
      "emailAddress" : "[email protected]", 
      "password" : "[email protected]", 
      "userAccessType" : "admin" 
     } 
    } 
} 

OR

db.collection.aggregate([ 
    { 
     $unwind:"$organizationInformation.users" 
    }, 
    { 
     $match:{ 
      "organizationInformation.users.emailAddress":"[email protected]", 
      "organizationInformation.users.password":"[email protected]" 
     } 
    }, 
    { 
     $project:{ 
      user: "$organizationInformation.users" 
     } 
    } 
]) 

結果は次のとおりです。

{ 
    "_id" : ObjectId("58f5e68c8205281d68bbb290"), 
    "user" : { 
     "firstName" : "testuser1", 
     "emailAddress" : "[email protected]", 
     "password" : "[email protected]", 
     "userAccessType" : "admin" 
    } 
} 
+0

ありがとうございます。 –

関連する問題