Click here to Skip to main content
15,896,436 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all, I am very new to mongodb and got stuck to a problem.
i have a collection as shown below

{                                                                                                                                                                          
        "_id" : ObjectId("5881b26f2694efd0ef17eb8b"),                                                                                                                      
        "firstName" : "sumit",                                                                                                                                             
        "lastName" : "vishwakarma",                                                                                                                                        
        "likes" : [                                                                                                                                                        
                {                                                                                                                                                          
                        "id" : 1,                                                                                                                                          
                        "value" : "java"                                                                                                                                   
                },                                                                                                                                                         
                {                                                                                                                                                          
                        "id" : 2,                                                                                                                                          
                        "value" : "c++"                                                                                                                                    
                }                                                                                                                                                          
        ]                                                                                                                                                                  
}                                                                                                                                                                          
{                                                                                                                                                                          
        "_id" : ObjectId("5881baf82694efd0ef17eb8c"),                                                                                                                      
        "firstName" : "test",                                                                                                                                              
        "lastName" : "test",                                                                                                                                               
        "likes" : [                                                                                                                                                        
                {                                                                                                                                                          
                        "id" : 3,                                                                                                                                          
                        "value" : "java"                                                                                                                                   
                },                                                                                                                                                         
                {                                                                                                                                                          
                        "id" : 2,                                                                                                                                          
                        "value" : "c++"                                                                                                                                    
                }                                                                                                                                                          
        ]                                                                                                                                                                  
}   

i want to write a query which give members and search for a particular id in likes and if that id is present then it should be true else false.
below is how the output should look
search for id:3 in likes

{                                                                                                                                                                          
        "_id" : ObjectId("5881b26f2694efd0ef17eb8b"),                                                                                                                      
        "firstName" : "sumit",                                                                                                                                             
        "lastName" : "vishwakarma",                                                                                                                                        
        "likes" : [                                                                                                                                                        
                {                                                                                                                                                          
                        "id" : 1,                                                                                                                                          
                        "value" : "java"                                                                                                                                   
                },                                                                                                                                                         
                {                                                                                                                                                          
                        "id" : 2,                                                                                                                                          
                        "value" : "c++"                                                                                                                                    
                }                                                                                                                                                          
        ]  ,
         "isLiked":false                                                                                                                                                       
}                                                                                                                                                                          
{                                                                                                                                                                          
        "_id" : ObjectId("5881baf82694efd0ef17eb8c"),                                                                                                                      
        "firstName" : "test",                                                                                                                                              
        "lastName" : "test",                                                                                                                                               
        "likes" : [                                                                                                                                                        
                {                                                                                                                                                          
                        "id" : 3,                                                                                                                                          
                        "value" : "java"                                                                                                                                   
                },                                                                                                                                                         
                {                                                                                                                                                          
                        "id" : 2,                                                                                                                                          
                        "value" : "c++"                                                                                                                                    
                }                                                                                                                                                          
        ],
         "isLiked":true
}   


any hint will be appreciated

What I have tried:

i have tried writing query using aggregation, $elemMatch but couldn't get the desired output
Posted
Comments
José Amílcar Casimiro 20-Jan-17 9:51am    
Read this: https://docs.mongodb.com/manual/aggregation/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900