Mongodb aggregatin查询 [英] Mongodb aggregatin query

查看:92
本文介绍了Mongodb aggregatin查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我对mongodb很新,遇到问题。

i有一个如下所示的收藏品



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希望编写一个查询,为成员提供并搜索喜欢的特定ID,如果该ID存在则应该其他是假的。

以下是输出应该如何看待

搜索id:3 in likes




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
}   





任何提示都将不胜感激



我的尝试:



i尝试使用聚合编写查询,$ elemMatch但无法获得所需的输出



any hint will be appreciated

What I have tried:

i have tried writing query using aggregation, $elemMatch but couldn't get the desired output

推荐答案

elemMatch但是无法获得所需的输出
elemMatch but couldn't get the desired output


这篇关于Mongodb aggregatin查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆