mongodbcurlmongohq

Setting Query Parameter on MongoHQ beta API


I'm using the beta api on MongoHQ.com to query a database I set up earlier this week.

I can retrieve the list of databases on my account, the collections in each database and the documents in each collection using the basic URLs.

However, when I try to set a query, limit, fields, etc parameter, it's returning a 404 error.

This is the curl command I'm using:

curl -i -X GET 'https://beta-api.mongohq.com/mongo/<deployment id>/<database name>/collections/<collection name>/documents?query={"name": "Name"}' \
  -H 'Content-Type: application/json' -H 'Authorization: api-key <API KEY>'

According to the documentation on the site here - https://docs.mongohq.com/mongohq-api-beta/mongodb-documents.html - the query has to be a "URL encoded MongoDB Extended JSON document". Unfortunately there are no examples of this.

How would I convert the query document above to a URL encoded one?


Solution

  • You can use curl to urlencode your query like this:

    curl -i -X GET 'https://beta-api.mongohq.com/mongo/<deployment id>/<database name>/collections/<collection name>/documents' \ 
      -G --data-urlencode 'query={"name": "Name"}' \
      -H 'Content-Type: application/json' -H 'Authorization: api-key <API KEY>'
    

    If you use operators like e.g. $gt, be sure to put double quotes around them:

    curl -i -X GET 'https://beta-api.mongohq.com/mongo/<deployment id>/<database name>/collections/<collection name>/documents' \ 
      -G --data-urlencode 'query={"value": { "$gt" : 10}}' \
      -H 'Content-Type: application/json' -H 'Authorization: api-key <API KEY>'