locked
Aggregation returns Can't convert from BSON type to date Error RRS feed

  • Question

  • The following aggregation runs on a MongoDB instance and it appears each stage and operation is supported by CosmosDB.

    The documents in my collection look like the following:

    {
      _id:5eb465210e56cee76582f9a3,
      tz:"America/New_York",
      islanderName:"Aydrian",
      islandName:"Echo Creek"
    }

    I'm trying to run the following aggregation:

    [
      {
        $addFields: {
          currentHour: {
            $hour: {
              date: new Date(now),
              timezone: "$tz",
            },
          },
        },
      },
      {
        $match: {
          currentHour: 16,
        },
      },
    ]

    With a UTC date string as the now variable.

    When I try to run this against CosmosDB I get the Can't convert from BSON type to date Error. Is this a bug? Is something not supported? Is there a work around? Thanks

    Thursday, May 7, 2020 9:52 PM

All replies

  • I think the now aggregation is supported in MongoDB Version 4 and above

    Azure Cosmos DB Mongo API is either 3.4 or 3.6 depending on the API version you provisioned your account with.

    Could you please try new Date() instead of new Date(now) ?

    -

    If this answers your query, do click “Mark as Answer” and Up-Vote for the same which might be beneficial to other community members reading this thread .

    And, if you have any further queries do let us know. 


    Monday, May 11, 2020 1:43 PM
  • Changing it to new Date() returns the same error.

    Also, this is what now is

    const now = new Date().toUTCString();

    • Edited by Aydrian Monday, May 11, 2020 6:17 PM
    Monday, May 11, 2020 5:51 PM
  • Hi Aydrian,

    I see this forum doesn't appear to be answered. Are you looking for a solution to this issue or did you resolve your issue? I can present this to the product group should you need additional help with this. 

    Regards,

    Mike

    Sunday, May 17, 2020 3:40 AM