How to select only matched sub-document of mongodb using python

前端 未结 2 1613
深忆病人
深忆病人 2021-01-03 17:14
Document in Mongo ->

            {
             emp_id : 11
             proj_info : 
                    [{
                     proj_id : 91
                           


        
相关标签:
2条回答
  • 2021-01-03 17:37

    welcome to Stackoverflow, you can use ${project} method to reduce it to the scope of the subdocument and then you can use ${match} there, like this:

    collectionObject.aggregate([
    {'$match':"emp_id":'11'}},
    {'$project': {"proj_info": 1}},
    {'$match':"proj_id":'91'}},
    {'$project': {"proj_team": 1}},
    {'$match': {"member_emp_id":'55'}}])
    
    0 讨论(0)
  • 2021-01-03 18:02

    Thanks but still I had to make some changes as it was not giving expected output, I made these changes =>

    collectionObject.aggregate([
    {'$match':{'emp_id': '11', "proj_info.pro_id": '11'   }},
    
    {'$unwind': "$proj_info"},
    
    {'$match': {"proj_info.proj_team.member_emp_id": '55' }},
    
    {'$unwind': "$proj_info.proj_team"},
    
    {'$project': {"proj_info.proj_team": 1,"_id" : 0}}
    
    ]):
    

    and this worked for me.

    Thanks for the help.

    0 讨论(0)
提交回复
热议问题