Find() with Data API and a Timeseries Collection [NOT WORKING]

I currently have a time series collection and I am exposing it with the Atlas Data API.

This is the general schema of my documents

{
            "time": "2022-01-05T09:34:00.000Z",
            "coin": "avax",
            "_id": "61d566111c94337a77a8bdbb",
            "value": 3.64,
            "currency": "usd"
}

Now I am able to filter my query with the ‘time’ field when

  • I am using MongoDB NodeJS driver
  • However when I try to run the same query using Data API, it doesn’t work (it doesn’t return any result when with the NodeJS driver it does).

MongoDB NodeJS Example

const result = await coins.find({
    coin: coin,
    time: {
      "$gte": new Date('2022-01-04')
    }
  }).toArray();

Same Example using the Data API

export const fetchCall = async (type, filter) => {
    let data = {
        "collection": "Coins",
        "database": "Prices",
        "dataSource": "DataAPITestCluster",
        "filter": {
            time: {
                "$gt": new Date('2022-01-04')
            }
        }
    };
    let config = {
        method: 'post',
        url: `https://data.mongodb-api.com/app/data-ycafa/endpoint/data/beta/action/${type}`,
        headers: {
            'Content-Type': 'application/json',
            'Access-Control-Request-Headers': '*',
            'api-key': key
        },
        data: data
    };
    return axios(config)
}

Is this an issue with the Data API not recognizing ‘date’ types?

Hi @Salman_Alam ,

I believe according to documentation you suppose to stringify the data object before passing it to data api:

let data = JSON.stringify( {
        "collection": "Coins",
        "database": "Prices",
        "dataSource": "DataAPITestCluster",
        "filter": {
            time: {
                "$gt": new Date('2022-01-04')
            }
        }
    });

You will see this in every node js test command provided by the UI examples…

Thanks
Pavel

Hi @Pavel_Duchovny Thanks for the response. I tried “stringifying” but it still didn’t work.

What is the error you get?

Oh I see the problem.

You must specify dates in extended json format with a timestamp only :

The above link has an example.

Thanks
Pavel

Hi Pavel,

Could you please provide an syntax example of what you mean? I’m having the same issue and am struggling to make it work.

How exactly does the following original poster’s code need to be modified so that a range of documents in between the two dates is returned ??

export const fetchCall = async (type, filter) => {
    let data = {
        "collection": "Coins",
        "database": "Prices",
        "dataSource": "DataAPITestCluster",
        "filter": {
            time: {
                 $gte: Date("2022-08-07"),
                 $lte: Date("2022-09-14")
            }
        }
    };
    let config = {
        method: 'post',
        url: `https://data.mongodb-api.com/app/data-ycafa/endpoint/data/beta/action/${type}`,
        headers: {
            'Content-Type': 'application/json',
            'Access-Control-Request-Headers': '*',
            'api-key': key
        },
        data: data
    };
    return axios(config)
}

Please advise.

Hi @Josh_Wipf

Please try:

 "filter": {
            time: {
                 $gte: {"$date": "2022-08-07T00:00:00.000Z"},
                 $lte: {"$date": "2022-09-14T00:00:00.000Z"}
            }
        }
1 Like