E
E
Egorian2018-03-13 19:56:47
Python
Egorian, 2018-03-13 19:56:47

MongoDb. How to find dictionary element by key value?

db = client.test
collection=db.test_collection
test_inf={"test1":"test2"}
collection.save(test_inf)
if collection.find(["test1"]):print("yes")

I can't figure out how to find an element by key without entering the item key?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eternalSt, 2018-03-14
@eternalSt

If I understand the problem correctly, then you need to use the $exists operator link to the dock
Example code:

db = client.test
collection=db.test_collection
test_inf={"test1":"test2"}
collection.save(test_inf)
// применяем оператор $exists 
collection.find({ "test1" : { "$exists" : true } })

PS maybe I don't know something, and/or I don't have the right mongo.
// Но вот так не работает
if collection.find({ "test1" : { "$exists" : true } }):print("yes")

// А так работает
collection.find({ "test1" : { "$exists" : true } }) && print("yes")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question