stored procedures – Cosmos DB: getContext().getCollection().queryDocuments does not return items in SP


I just started with Cosmos DB and I am left puzzled why I won’t get any documents returned using a simple SP query.

This is what I have done:

  • created Cosmos DB database with partition key “/id”

  • created catalogue “Families”

  • created two items:

    {
    “id”: “AndersenFamily”,
    “lastName”: “Andersen”,
    “parents”: (
    {
    “firstName”: “Thomas”
    },
    {
    “firstName”: “Mary Kay”
    }
    ),
    “children”: (
    {
    “firstName”: “Henriette Thaulow”,
    “gender”: “female”,
    “grade”: 5,
    “pets”: (
    {
    “givenName”: “Fluffy”
    }
    )
    }
    ),
    “address”: {
    “state”: “WA”,
    “county”: “King”,
    “city”: “Seattle”
    },
    “creationDate”: 1431620472,
    “isRegistered”: true
    }

    {
    “id”: “WakefieldFamily”,
    “parents”: (
    {
    “familyName”: “Wakefield”,
    “givenName”: “Robin”
    },
    {
    “familyName”: “Miller”,
    “givenName”: “Ben”
    }
    ),
    “children”: (
    {
    “familyName”: “Merriam”,
    “givenName”: “Jesse”,
    “gender”: “female”,
    “grade”: 1,
    “pets”: (
    {
    “givenName”: “Goofy”
    },
    {
    “givenName”: “Shadow”
    }
    )
    },
    {
    “familyName”: “Miller”,
    “givenName”: “Lisa”,
    “gender”: “female”,
    “grade”: 8
    }
    ),
    “address”: {
    “state”: “NY”,
    “county”: “Manhattan”,
    “city”: “NY”
    },
    “creationDate”: 1431620462,
    “isRegistered”: false
    }

  • running query “SELECT * FROM c” returns both records as expected

  • However, the queryDocuments method in below SP always retrieves zero length for feed. Why aren’t both items returned?

    // SAMPLE STORED PROCEDURE
    function sample(prefix) {
        // Query documents and take 1st item.
        var isAccepted = getContext().getCollection().queryDocuments(
            getContext().getCollection().getSelfLink(),
            'SELECT * FROM root r',
        function (err, feed, options) {
            if (err) throw err;
    
            console.log("Feed length: " + feed.length);
    
            // Check the feed and if empty, set the body to 'no docs found', 
            // else take 1st element from feed
            if (!feed || !feed.length) {
                var response = getContext().getResponse();
                response.setBody('no docs found');
            }
            else {
                var response = getContext().getResponse();
                var body = { prefix: prefix, feed: feed(0) };
                response.setBody(JSON.stringify(body));
            }
        });
    
        if (!isAccepted) throw new Error('The query was not accepted by the server.');
    }

Many thanks for your help!