In MongoDB how to return only part of array?

前端 未结 2 2004
臣服心动
臣服心动 2020-12-21 01:42

Consider collection \"fruits\", in which I have this document (I\'m using Python\'s pymongo driver, btw):

{
    \'_id\'       : \'lemons\',
    \'weight\'            


        
相关标签:
2条回答
  • 2020-12-21 02:14

    Have you tried adding another inclusion projection? I think you may be able to add something trivial like foo:1 (that is not a real field) and it should work.

    Like so:

    { 'countries' : { '$slice' : [0, 3] }, '_id' : 0, foo : 1 }
    

    If it doesn't work I suggest filing a bug with mongo. They are actually very good about responding to bugs.

    0 讨论(0)
  • 2020-12-21 02:26

    That's strange because that's not the behavior when running in the mongo console.

    Have you tried putting 'lemons' in curly braces so it's valid JSON syntax?

    find_one({'_id':'lemons'}, { 'countries' : 1, '_id' : 0 })
    

    It looks to me like it's applying your $slice projection to all of the fields, so it could be some weird syntax issue with the pymongo driver

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