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 the collection I need to retrieve documents where userList. loginame="[email protected]" also check where role is "admin". Where role is admin means retrieve all userList:LoginName with their role, else retrieve only userList:loginName with their role, what ever it is.

I tried this:

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

i need output like this

{

    "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"
        }

    ]
}
2
  • So you need to find all the documents with loginName as [email protected] ? Why not try db.Site.find({"userList.loginName": "[email protected]"}) ? Commented Mar 1, 2017 at 10:31
  • yes.loginName as [email protected] Commented Mar 1, 2017 at 10:34

1 Answer 1

3

Use this command ,

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

Hope this will help you.

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());
                }   
Sign up to request clarification or add additional context in comments.

3 Comments

how i can implement this query in java
hi @radhaKrishnan i need some mongodb help can you me your mail id to contact you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.