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

Viewed 117

in my database, there are two companies with same name.

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

like this..

my question is, I juse made my python function to get store information by name.

that looks like this..

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

and I can only get the first lg company's information. how can I get both informations of lg company from my mongodb?

4 Answers

Using variables such as name, address, zip code to identify a document is not a good approach as there may be the same names, names with different cases, spaces ex: LG, lg.

You can use the default mongo _id as a unique property to find the doc. Still you want to get the document then use collection.find({"name":store_name}) it will returns multiple documents ab array. If you want to retrieve only one doc then use collection.findOne({"name":store_name})

Related