MongoDB in CosmosDB: Rest API querying

John J 96 Reputation points
2021-09-06T02:52:06.057+00:00

Hey guys,
I setup a MongoDB instance in Cosmos.
To be honest I've used SQL Server for years, but I decided to give MongoDB a shot and only a few days in, I'm really liking the conventions and whatnot.

Anyway, I've been working with a MongoDB library for the last little while and everything's been working great.
However now I'm at a place where I want to query my MongoDB from a Cloudflare worker.
Cloudflare workers, if you don't know, are intended for fairly small scripts and they are limited to HTTP/HTTPS requests. The native mongo protocol is not supported.
The logical response would be to redo everything as a "Core" CosmosDB, but I'm actually liking MongoDB a lot, and I'd like to get it working if possible.

In Azure I noticed the JSON properties on my DB:

"properties": {
"provisioningState": "Succeeded",
"documentEndpoint": "https://redacted.documents.azure.com:443/",
"mongoEndpoint": "https://redacted.mongo.cosmos.azure.com:443/",

Which seemed to indicate the MongoDB has multiple endpoints. Using a fairly old Postman collection I'd found for CosmosDB, I was able to successful authenticate and perform some RESTful commands against my DB using the "documentEndpoint" URL. Even a "SELECT * from mycollection" seemed to work. Cool.

However, for the life of me I have no idea how to properly construct a query with a WHERE clause that actually returns anything.

For example in Mongo I have a query like:

db.getCollection('transfers').find({blockNumber:9673998})

Which returns a single document.

From Postman, In my POST request if I do:

{  
    "query": "SELECT * FROM transfers WHERE blockNumber = 9673998"
}  

I get the response:

{
    "code": "BadRequest",
    "message": "Message: {\"errors\":[{\"severity\":\"Error\",\"location\":{\"start\":30,\"end\":41},\"code\":\"SC2001\",\"message\":\"Identifier 'blockNumber' could not be resolved.\"}]}\r\nActivityId: bdd28ddb-732c-43cc-9759-6a0fe36a75b4, Microsoft.Azure.Documents.Common/2.14.0"
}

If I do:

{  
    "query": "SELECT * FROM transfers t WHERE t.blockNumber = 9673998"
}  

I don't get an error, but I get zero results:

{
    "_rid": "3rJbAKftSvY=",
    "Documents": [],
    "_count": 0
}

I've tried passing values as parameters and quite a few other things.
I think I've tried a hundred different variations to the point where I'm at a loss.
Maybe I'm just venturing some the path of something that's not actually supported?
If anyone has any tips for me to get this to work, that would be great.

Thanks!

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,506 questions
{count} votes

Accepted answer
  1. John J 96 Reputation points
    2021-09-06T17:40:58.307+00:00

    I think I figured it out.

    The query that finally worked for me was

    {    
        "query": "SELECT * FROM transfers t WHERE t['$v']['blockNumber']['$v'] = 9673998"  
    }  
    

    129600-screen-shot-2021-09-06-at-13755-pm.png


0 additional answers

Sort by: Most helpful