2017-03-01 60 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" 
      } 
     ] 
    } 

在集合中,我需要檢索文檔,其中userList. loginame="[email protected]"也檢查角色是「admin」的位置。角色是admin的意思是用他們的角色檢索所有userList:LoginName,否則只檢索userList:loginName和他們的角色,它究竟是什麼。Retrive嵌套數組文件

我嘗試這樣做:

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

我需要這樣的

{ 

    "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

所以,你需要找到所有與'loginName'爲'venkat @ gmail.com'文件?爲什麼不試試'db.Site.find({「userList.loginName」:「[email protected]」})'? – Veeram

+0

yes.loginName as [email protected] – venkat

回答

3

輸出使用此命令,

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

OutputData:

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

希望這會對你有所幫助。

Java代碼:

   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

感謝很多....它的工作 – venkat

+0

我怎麼能在java中實現這個查詢 – venkat

+0

嗨@radhaKrishnan你很好,謝謝你...... – venkat