简体   繁体   中英

Why my query using find() does not return any document?

I want to make a query on mongo to get all the documents with three conditions : In python code I have a dynamic value type which can be changed it can have different values. And I want to get all documents with type and that don't have field delete and inappropriate undefined.

So I started like this, here I call it:

find_entry_based_on_url({"type": "Test"})

Here is the method that I defined :

def find_entry_based_on_url(self, query):
    docs = self.mongo.db[self.collection_name].find(query , {"delete": {'$exists': False}} , {"inappropriate": {'$exists': False}})
    return docs

This is not working it gives nothing ? Can you help me what is wrong?

You are doing it wrong the second argument to find is a projection document. Also in PyMongo, documents are regular Python dictionary which mean that there are mutable thus you can add additional key to your query.

from copy import deepcopy #  You need to make a deepcopy of original query


def find_entry_based_on_url(self, query):        
    q = deepcopy(query)
    extra_keys = ['delete', 'inappropriate']
    for key in extra_keys:
        q[key]['$exists'] = False
    return self.mongo.db[self.collection_name].find(q)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM