【问题标题】:python3 pymongo find and array_filters [duplicate]python3 pymongo查找和array_filters [重复]
【发布时间】:2018-10-26 15:50:49
【问题描述】:

我知道 Pymogno update_many 方法支持 array_filter。 但 find 方法不支持 array_filter。我该怎么办?

这是我的 json:

[{'milestones': [{'content': 'The idea of multiwallet + debit card integration '
                         'was born. ',
                  'title': 'September, 2017 '},
                 {'content': 'The team of 10 professionals, including coders, '
                         'designer, lawyer and CFO was\r\n'
                         'founded. ',
                 'title': 'September, 2017-January 2018'},
                {'content': 'Deal with Estonian private-owned bank, issuing '
                         'pre-paid Mastercard® for\r\n'
                         'ZANTEPAY.',
                 'title': 'February 1st, 2018'}]}]

这是我的代码:

db = MongoClient("localhost:27017")
db.collection.find({'milestones.title':{'$regex':'September'}},{'milestones.$.title'})

结果:

[{'milestones': [{'content': 'The idea of multiwallet + debit card integration '
                             'was born. ',
                  'title': 'September, 2017 '}]}]

我想要的结果如下:

[{'milestones': [{'content': 'The idea of multiwallet + debit card integration '
                         'was born. ',
                  'title': 'September, 2017 '},
                 {'content': 'The team of 10 professionals, including coders, '
                         'designer, lawyer and CFO was\r\n'
                         'founded. ',
                 'title': 'September, 2017-January 2018'}]}]

我想得到一些建议。 谢谢。

【问题讨论】:

  • 很确定我以前见过你发布这个问题,你会被指向同一个副本。您被指向重复项是因为您所问问题的解决方案就在那里,而不是因为我们只想结束您的问题。我们实际上是在向您展示答案并提供帮助。如果您认为您之前没有发布过问题,那么这一定是对使用与数组过滤器搜索完全相同的措辞的其他人的简单误解。尽管如此,没有必要再次发布相同的问题,因为已有答案可供学习。

标签: arrays python-3.x mongodb filter pymongo


【解决方案1】:

您可以为此使用aggregate。我仍然是 MongoDB 的初学者,仍在从其他人的问题中学习。 :)

db.collection.aggregate([
    {"$unwind": "$milestones"},
    {"$match": {"milestones.title": {"$regex": "September"}}},
    {"$group": {
        "_id": "$_id",
        "milestones": {
            "$push":  {
                "content": "$milestones.content",
                "title": "$milestones.title"
            }
        }
    }
    }
])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 2019-07-30
    • 2018-01-13
    • 1970-01-01
    • 2017-06-18
    相关资源
    最近更新 更多