2017-04-18 5 views
1

Ich habe das Dokument in Mongo-Sammlung namens (CustomerInformation) mit folgender Struktur.Spring Daten-Mongo DB Query Embedded Array

 {  "_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" 
        } 
       ] 
      } 
     } 

Jetzt möchte ich nur die Benutzerinformationen mit übereinstimmenden emailAddress und Passwort abrufen. Ich versuche es wie folgt.

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); 

ich das vollständige Dokument mit allen Benutzern Array bin immer, ich möchte nur Informationen und emailaddress Passwort passenden Benutzer abzurufen. Was ist falsch in meiner Abfrage oder Datenmodell? Irgendwelche Vorschläge? Vielen Dank!

Antwort

0

Positionale Projektion verwenden.

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

Danke. Es hat funktioniert .. –

0

Sie können Aggregationsanfrage mit $ entspannen Sie mit diesem

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

Ergebnis zu erreichen ist:

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

ODER

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

Ergebnis ist:

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

Vielen Dank .. Es hat funktioniert. –