簡體   English   中英

如何用Pymongo查詢mongodb同一個字段根據多個條件得到結果

[英]How to query mongodb with Pymongo to get results based on multiple conditions on the same field

我有一個 mongo 集合,其中包含如下數據:

{'email': 'test1@gmail.com'}
{'email': 'test2@gmail.com'}
{'email': 'test3@gmail.com'}

我想得到 email 的結果,即test1@gmail.comtest2@gmail.com ,例如:

[{'email':'test1gmail.com'},{'email':'test2gmail.com'}]

有沒有辦法用 Pymongo 的 find 做到這一點?

我試過使用:

test.find({'email': 'test1@gmail.com', 'email':'test2@gmail.com'})

但我認為這只會得到 email test2@gmail.com而不是'test1@gmail.com 如果我做:

test.find({'email': 'test2@gmail.com', 'email':'test1@gmail.com'})

這只會給我test1@gmail.com的結果。

以為我會在多個條件下基於 Mongo DB 查詢給出$or嘗試:

test.find({"$or":[{'seller_email': 'test2@gmail.com', 'seller_email':'test1@gmail.com'}]})

但這似乎也不起作用。

我查看了文檔: https://pymongo.readthedocs.io/en/stable/tutorial.html ,但我沒有看到。

我知道你可以從 mongo 的 cli: MongoDB query with multiple conditions做到這一點,但我需要它通過 pymongo 來做到這一點。

我想我發現了問題,使用$or關鍵字應該有效。 我遺漏/放錯了一些{}

應該:

test.find({"$or":[{'seller_email': 'test2@gmail.com'}, {'seller_email':'test1@gmail.com'}]})

代替:

test.find({"$or":[{'seller_email': 'test2@gmail.com', 'seller_email':'test1@gmail.com'}]})

我覺得你最好參考mongodb官方的幫助頁面,查詢方法看這里: https://www.mongodb.com/docs/manual/tutorial/query-documents/參考'Or'條件我相信會解決你的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM