0

我目前正在使用主索引来查询 Cloudant 数据库中的键列表:

class DAO:

    @staticmethod
    def get_movie_names(movie_ids: List[int]) -> Dict[int, str]:

        # The movie_ids in cloudant are stored as strings so convert to 
        # correct format for querying
        movie_ids = [ str(id) for id in movie_ids ]

        keys = urllib.parse.quote_plus(json.dumps(movie_ids))

        # The movie id is stored in the _id field, so we query it 
        # using the 'keys' parameter
        end_point = '{0}/{1}/_all_docs?keys={2}&include_docs=true'.format (
                                                      CL_URL, CL_MOVIEDB, keys
                                                      )
        response = cloudant_client.r_session.get(end_point)
        movie_data = json.loads(response.text)

        movie_names = {}

        if 'rows' in movie_data:
            for row in movie_data['rows']:
                if 'doc' in row:
                    movie_id   = int(row['key'])
                    movie_name = row['doc']['name']
                    movie_names[movie_id] = movie_name
        return movie_names

看起来我可以使用cloudant.result.Result来实现这一点。如果我正确理解了文档,这将返回所有文档,然后您可以过滤返回的结果。但是,我想通过将参数传递给 Cloudant 请求进行过滤,以便只返回我感兴趣的数据。这可能吗?

4

1 回答 1

3

Chris - CloudantDatabase gives you access to all_docs, is that what you want?

http://python-cloudant.readthedocs.io/en/latest/database.html#cloudant.database.CouchDatabase.all_docs

于 2017-01-13T11:20:47.880 回答