2

I'm using the CosmosDB noSQL API and I feel like I'm not doing it right. The methods in the container class all return these iterator objects, which only have one method: next().

I need to get a list of records out of my container, and my only thought is to do this:

def to_list(iterator):
    list = []
    for i in range(0, 99):
        try:
            list.append(iterator.next())
        except:
            break
    return list

This works, but it feels bad. Is this going to be a performance problem? Does the Iterator class actually have all of my data in it? Like if I do the above with an iterator I get back, am I making 1 query to cosmos or 100?

I found this article which is using the methods in azure.cosmos.cosmos_client instead, but you still end up looping over everything. Is this normal, should I be using sqlalchemy or something instead?

What's best practice for how to talk to CosmosDB using python?

1 Answer 1

2

If you're trying to read all items in a container you can use this method.

item_list = list(container.read_all_items(max_item_count=10))

and this for a query.

items = list(container.query_items(
        query="SELECT * FROM r WHERE r.name=@name",
        parameters=[
            { "name":"@name", "value": "Mark" }
        ],
        enable_cross_partition_query=True
    ))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.