-2

I am trying to update an existing dictionary with a new key-value pair.But the update dictionary adds single quotes to the value field. This dictionary is to be used for mongo query and hence needs to be clean of additional quotes whenever I substitute the value variable in dictionay.update(key:value)

I tried using eval and json.loads. But that was not fruitful. Any advices please.

mquery ={"type" : "search"}

skey = "ZXCV,YBC"

skey = '{"$in": [/' + skey.replace(',','/,/') + '/]}'

print(skey)

if skey is not None : mquery.update({"skey1" : skey})

print(mquery)

Expected - {'type': 'search', 'skey1': {"$in": [/ZXCV/,/YBC/]}}

Current - {'type': 'search', 'skey1': '{"$in": [/ZXCV/,/YBC/]}'}

Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119
  • 1
    in fact expect output is not valid dict in this part `[/ZXCV/,/YBC/]` – buran Jun 04 '19 at 18:56
  • 1
    Why are you making `skey` a string in the first place, if you want the value to be a `dict`? Your expected dict isn't even valid: do you want `'[/ZXCV/,/YBC/]'` or `['/ZXCV/', '/YBC/']` instead? – chepner Jun 04 '19 at 18:56
  • The single quotes in this printed output `'{"$in": [/ZXCV/,/YBC/]}'` are not part of the string. They are part of what python displays when displaying a string in a dictionary. If you were to do `print(mquery['skey1'])` then the displayed value would be `{"$in": [/ZXCV/,/YBC/]}`. If you pass this value to a mongo query it will not have quotes. The quotes are just for making the display easier to read. – Steven Rumbalski Jun 04 '19 at 19:03
  • This is being used to query MongoDB **db.qu1.find({'type': 'search', 'skey1': {"$in": [/ZXCV/,/YBC/]}} ** But with the output that I am getting(with additional quotes ) the query fails to run. – saahil shah Jun 04 '19 at 19:16
  • You are confusing an object's representation with it's value. Most python containers, including dictionaries, will show the representations (aka __repr__) of the objects it contains. Strings are represented with quotes around them. But here's the import part! Those surrounding quotes aren't part of the string's value. For proof of this try `print(mquery['skey1'][0])` and you will find the output is `{` not `'` as you would expect if quotes were part of the string. – Steven Rumbalski Jun 04 '19 at 19:23
  • Thanks for explaining the part @StevenRumbalski , But I am querying the mongoDB and getting no results and If i remove the quotes manually, result comes up. Not able to get round this issue. The issue is quotes embracing the value in the query – saahil shah Jun 04 '19 at 19:29
  • `db.qu1.find({'type': 'search', 'skey1': {'$in': [re.compile('.*ZXCC.*'), re.compile('.*YBC.*')]}}` but don't forget to first `import re`. See [PyMongo $in + $regex](https://stackoverflow.com/questions/19867389/pymongo-in-regex) and [Pymongo $in Query Not Working](https://stackoverflow.com/questions/38919756/pymongo-in-query-not-working). – Steven Rumbalski Jun 04 '19 at 19:47
  • Thank you so much that is the answer to the point. 5stars.. – saahil shah Jun 04 '19 at 20:34

1 Answers1

0

import re

db.qu1.find({'type': 'search', 'skey1': {'$in': [re.compile('.ZXCV.|.YBC.')]}}

This is the answer with RE.