search values in mongo db

Viewed 30

i have a number of documents in collections as like

{"_id":{"$oid":"62336c9252d80fbc8b25b908"},
"stock_id":1,
"barcode":"SAR001MAD",
"main_type":"Saree",
"sub_type":"Madurai cotton",
"quantity":20,
"actual_price":***,
"sellig_price":***,
"discount":20,
"date":"2021-12-07"}

{"_id":{"$oid":"62336c9252d80fbc8b25b909"},
"stock_id":2,
"barcode":"SAR002VIN",
"main_type":"Saree",
"sub_type":"Vinar",
"quantity":67,
"actual_price":***,
"sellig_price":***,
"discount":20,
"date":"2021-12-07"}

I have nearly 1000 collections like above lastly used MySQL query as

query ="select * from stock where stock_id = '{}' or barcode = '{}' or main_type like'{}%'"\ 
"or sub_type like '{}%' or main_type like '%{}%' " \
"or sub_type like '%{}%'".format(self.value, self.value, self.value, self.value, self.value, self.value)

I would get the output as required data needed

But how to get the nearly same query in pymongo

I scripted the Pymongo scripts as

def stock_search(self, value):
    self.value = value
    code = # will get some return from method
    col = clint['Shop']['Stock']
    data = col.find({"$or": [{"stock_id": self.value}, {"barcode": code}, 
    {"main_type": self.value}]})

in this main issue is when the value is string in 'stock_id' it wont take string as value can any please suggest a solution for this

0 Answers
Related