2017-03-01 6 views
0
{ 

     "id" : "Sir3GHMQ", 
     "name" : "Medavakkam", 
     "userList" : [ 
      { 
       "loginName" : "[email protected]", 
       "role" : "ADMIN" 
      }, 
      { 
       "loginName" : "[email protected]", 
       "role" : "Operator" 
      } 
     ] 
    } 


    { 

     "id" : "Sir3GHER", 
     "name" : "Medavakkam", 
     "userList" : [ 
      { 
       "loginName" : "[email protected]", 
       "role" : "OPERATOR" 
      }, 
{ 
       "loginName" : [email protected]", 
       "role" : "OPERATOR" 
      } 
     ] 
    } 

In der Sammlung muss ich Dokumente abrufen, wo userList. loginame="[email protected]" auch überprüfen, wo Rolle "admin" ist. Wo Rolle ist admin bedeutet, alle Benutzerliste: LoginName mit ihrer Rolle abzurufen, sonst nur Benutzerliste abrufen: LoginName mit ihrer Rolle, was immer es ist.Verschachteltes Array Dokument

Ich versuchte dies:

db.Site.aggregate([ 
    { "$match": { "userList.loginName": "[email protected]" } }, 
    { "$redact": { 
     "$cond": [ 
      { "$eq": [ 
       { "$ifNull" [ "$loginName", "[email protected]" ] }, 
       "[email protected]" 
      ] }, 
      "$$DESCEND", 
      "$$PRUNE" 
     ] 
    } } 
]) 

i Ausgang benötigen wie dieses

{ 

    "id" : "Sir3GHMQ", 
    "name" : "Medavakkam", 
    "userList" : [ 
     { 
      "loginName" : "[email protected]", 
      "role" : "ADMIN" 
     }, 
     { 
      "loginName" : "[email protected]", 
      "role" : "Operator" 
     } 
    ] 
} 


{ 

    "id" : "Sir3GHER", 
    "name" : "Medavakkam", 
    "userList" : [ 
     { 
      "loginName" : "[email protected]", 
      "role" : "OPERATOR" 
     } 

    ] 
} 
+0

Sie müssen also mit 'loginName' alle Dokumente finden, wie' venkat @ gmail.com'? Warum nicht versuchen, 'db.Site.find ({" userList.loginName ":" [email protected] "})' '? – Veeram

+0

yes.loginName als [email protected] – venkat

Antwort

3

Verwenden Sie diesen Befehl,

  db.f.aggregate([{ 
      $match: { 
       "userList.loginName": "[email protected]" 
      } 
     }, { 
      "$redact": { 
       "$cond": [{ 
        $or: [{ 
         "$eq": [{ 
           "$ifNull": ["$loginName", "[email protected]"] 
          }, 
          "[email protected]" 
         ] 
        }, { 
         "$eq": [{ 
          $setIsSubset: [{ 
           $literal: [{ 
            loginName: "[email protected]", 
            role: "ADMIN" 
           }] 
          }, "$$ROOT.userList"] 
         }, true] 
        }] 
       }, "$$DESCEND", "$$PRUNE"] 
      } 
     }]).pretty() 

Output:

 { 
    "_id" : ObjectId("58b697e406169b8451ba4cd2"), 
    "id" : "Sir3GHMQ", 
    "name" : "Medavakkam", 
    "userList" : [ 
      { 
        "loginName" : "[email protected]", 
        "role" : "ADMIN" 
      }, 
      { 
        "loginName" : "[email protected]", 
        "role" : "Operator" 
      } 
    ] 
    } 

    { 
    "_id" : ObjectId("58b74e91c568ace843ee17c1"), 
    "id" : "Sir3GHER", 
    "name" : "Medavakkam", 
    "userList" : [ 
      { 
        "loginName" : "[email protected]", 
        "role" : "OPERATOR" 
      } 
    ] 
} 

Hoffe das wird dir helfen.

Java-Code:

   MongoClient mongoClient = new MongoClient(); 
       MongoDatabase database = mongoClient.getDatabase("test"); 
       MongoCollection<Document> collection = database.getCollection("f"); 

       List<Document> results = collection.aggregate(Arrays.asList(new Document("$match",new Document().append("userList.loginName", "[email protected]")), 
         new Document("$redact", new Document("$cond", 
           Arrays.asList(new Document("$or",Arrays.asList(new Document("$eq", 
             Arrays.asList(new Document("$ifNull", Arrays.asList("$loginName", "[email protected]")), "[email protected]")),new Document("$eq", Arrays.asList(new Document("$setIsSubset", Arrays.asList(new Document("$literal", Arrays.asList(new Document().append("loginName", "[email protected]").append("role", "ADMIN"))),"$$ROOT.userList")), true)))), 
           "$$DESCEND", "$$PRUNE"))) 

      )).into(new ArrayList<Document>()); 

       for(Document docs: results){ 
        System.out.println(docs.toJson()); 
       } 
+0

danke viel .... seine Arbeit – venkat

+0

wie kann ich diese Abfrage in Java implementieren – venkat

+0

hi @radhaKrishnan Sie sind groß .thank Sie ...... – venkat

Verwandte Themen