mongodb-js / charts-embed-sdk

The easiest way to embed MongoDB Charts visualisations into your web app

Home Page:https://docs.mongodb.com/charts/master/embedding-charts-sdk/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support ObjectId filter type

cesargdm opened this issue · comments

Is your feature request related to a problem? Please describe.
I want to pass a ObjectId value to the EmbedChartOptions, if I just pass a string in the filter, I don't get the correct results since it expects an ObjectId.

Describe the solution you'd like
Create a ObjectId export or allow to parse ObjectId strings automatically.

Additional context

{
 filter: {
  products: { $in: ['5c06e8f5b5e7d33b306f6c92'] },
}

Does not work

Screen Shot 2022-05-29 at 8 24 14 PM

Works

Found a solution here, maybe a helper, would be useful, or at least some documentation https://stackoverflow.com/questions/67431949/how-to-filter-mongodb-charts-with-objectid

Hello @cesargdm, the filter option in EmbedChartOptions and the setFilter method in the SDK support all BSON types. Field types documentation is not directly related to Charts as it is a MongoDB thing. It is documented in multiple places, including here: https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-ObjectId. So for ObjectId filters in our embedding SDK you can use such expressions {_id: {"$oid":"5d505646cf6d4fe581014ab2"}}.

Another way you can use BSON types in your app is to import the npm bson library and use the ObjectId class. Example for a filter is { _id: new ObjectId("5d505646cf6d4fe581014ab2") }. This will also allow you to create valid objectIds in your code (if you need them).

Thank you for the feedback, we can include bson type filters in our examples of how to use the SDK.

Got it, thank you @kristinamongo !