簡體   English   中英

我如何從 mongodb 獲取所有具有相同值的數據?

[英]how can i get all the datas that have same value from mongodb?

在我的數據庫中,有兩家同名的公司。

[
  {
    "name": "samsung",
    "store_code": "34d"
  },
  {
    "name": "lg",
    "store_code": "333"
  },
  {
    "name": "lg",
    "store_code": "3511"
  }
]

像這樣..

我的問題是,我只是讓我的 python function 通過名稱獲取商店信息。

看起來像這樣..

async def fetch_store_by_name(store_name):
document = collection.find_one({"name":store_name})
return document

而且我只能得到第一家lg公司的信息。 我怎樣才能從我的 mongodb 獲得 lg 公司的信息?

我假設您正在使用pymongo ,只需將find_one更改為find方法。 像這樣:

async def fetch_store_by_name(store_name):
  document = collection.find({"name":store_name})
  return document

這里有一些文檔: https://pymongo.readthedocs.io/en/stable/tutorial.html#querying-for-more-than-one-document

您可以使用collection.find({"name":store_name})

參考: https://www.w3schools.com/python/python_mongodb_find.asp

使用名稱、地址、zip 代碼等變量來識別文檔並不是一個好的方法,因為可能存在相同的名稱、不同大小寫的名稱、空格,例如:LG、lg。

您可以使用默認的 mongo _id 作為唯一屬性來查找文檔。 你仍然想獲取文檔然后使用collection.find({"name":store_name})它將返回多個文檔 ab 數組。 如果您只想檢索一個文檔,請使用collection.findOne({"name":store_name})

暫無
暫無

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

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