Make a query with pymongo filtering by a string ignoring uppercase and minuscule letters

Asked

Viewed 33 times

0

I need to make a query in a database in Mongodb using the pymongo library in python, the query I am making is as follows:

dbCliente.find({"nome": "carlos"})

I want to return the client whose name is exactly "carlos" but ignoring capital letters or minuscules.

How do I make this query using pymongo?

  • Tries with regex :dbCliente.find({"nome":{ $regex: 'carlos', $options: 'i' } })

1 answer

1


The most correct way would be to create an index that is case insensitive

Creating the collection

db.createCollection("nomes", { collation: { locale: 'pt_BR', strength: 2 } })

Creating index

db.nomes.createIndex( { nome: 1} )

Inserting names

db.nomes.insert( [ { nome: "Carlos" },
                   { nome: "CARLOS"},
                   { nome: "carlos"} ] )

Seeking

db.nomes.find( { nome: "carlos" } )

Note: will find the three results

In your case, you can use

dbCliente.find({"nome": "carlos"})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.