0

I am having mongodb data like this

{
    "_id" : ObjectId("58eb32fb58bbf720a80daf6f"),
    "RoleName" : "Admin",
    "UIList" : [ 
        {
            "UiName" : "ManageRole",
            "View" : true,
            "Edit" : false,
            "_id" : ObjectId("58eb32fb58bbf720a80daf79")
        }, 
        {
            "UiName" : "ManageBuildings",
            "View" : false,
            "Edit" : false,
            "_id" : ObjectId("58eb32fb58bbf720a80daf78")
        }, 
        {
            "UiName" : "ManageFacility",
            "View" : true,
            "Edit" : false,
            "_id" : ObjectId("58eb32fb58bbf720a80daf77")
        }
    ],
    "__v" : 0,
    "Description" : "Full Control",
    "IsActive" : true
}

I want to retrieve only View:true in UIList under UiName:"ManageFacility" 's how to retrieve that.

I tried to get the data following this query but it retrieve all UIList Under Uiname:,View,Edit value.

db.getCollection("manage_roles_news").find(
    { _id:ObjectId("58eb32fb58bbf720a80daf6f")},
    {UIList: {$elemMatch: {UiName: "ManageFacility"}}});  

How to retrieve that only view in UIlist under Uiname:"ManageFacility"

can anyone tell me

3
  • Try db.getCollection("manage_roles_news").find( { _id:ObjectId("58eb32fb58bbf720a80daf6f")}, {UIList: {$elemMatch: {UiName: "ManageFacility", View:true}}}); Commented Apr 11, 2017 at 13:59
  • Hi, @Veeram this query also return all the managefacility column Commented Apr 11, 2017 at 14:00
  • I want only view Commented Apr 11, 2017 at 14:00

1 Answer 1

1

I don't think there is way to project individual fields from positional/elemMatch projection. It always resolves to array element.

You can try below aggregation to project field. The below query uses $filter operator to find the matching array values which is piped to $arrayAtElem to return the first element (0) and pass the matching document to $let operator and followed by picking the View property.

db.getCollection("manage_roles_news").aggregate(
  { $match: { _id: ObjectId("58eb32fb58bbf720a80daf6f") } }, 
  { $project: { 
      _id:0,
      View: 
      { $let: { 
            vars: { 
              mElement: { 
                $arrayElemAt: [{
                            $filter: {
                                input: "$UIList",
                                as: "fElement",
                                cond: {
                                    $eq: ["$$fElement.UiName", "ManageFacility"]
                                }
                            }
                        }, 0]
                    }
                },
            in: {$cond:["$$mElement.View", 1, 0]}
            }
        }
    }
})
Sign up to request clarification or add additional context in comments.

7 Comments

1. Add _id:0 to exclude _id in the $project stage. 2 Replace "$$mElement.View" with {$cond: [ "$$mElement.View", 1, 0 ] }
can post this in another answer
Sorry. I didn't understand you. Do you want me to update answer with the previous comment changes ?
now my requirement is: i want the count of table. i will give id value:"ObjectId("58eb32fb58bbf720a80daf6f")", UiName:"ManageFacilty",and View:"true". If the three conditions are satisfies return 1 or else. 0
db.getCollection("manage_roles_news").find( { _id:ObjectId("58eb32fb58bbf720a80daf6f")}, {UIList: {$elemMatch: {UiName: "ManageFacility", View:true}}});
|

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.