I'm trying to filter a chart in MongoDB by the organization ID of a user. The filter works fine with strings and numbers, but it doesn't work when I try to filter using ObjectIDs.
This gives back no results, likely because "organization" is an ObjectID;
const filter = { 'organization': user.organization }
This gives me an error (Error loading data for this chart (error code: -1). Unknown Error.);
const filter = { 'organization': {'$toObjectId': user.organization} }
This string filter works but isn't what I need;
const filter = { 'organization_name': 'test' }
This string filter works but isn't what I need;
const MongoChart = props => {
const { baseUrl, chartId, height, filter, getToken } = props;
const classes = useStyles();
const chartRef = React.createRef();
const sdk = new ChartsEmbedSDK({
baseUrl: baseUrl,
autoRefresh: true,
showAttribution : false,
maxDataAge: 100,
getUserToken: async () => {
const token = await getToken();
return token;
}
});
const chart = sdk.createChart({
chartId: chartId,
height: height,
filter: filter
});
useEffect(() => {
chart.render(chartRef.current)
})
return (
<div className={classes.root}>
<div className={classes.chart} ref={chartRef} />
</div>
);
}
export default MongoChart;
Is there a way to convert a string to a Mongoose ObjectID on the front end?
Just realized the syntax was wrong.
I switched '$toObjectId' to '$oid' and it works.
const filter = { 'organization': {'$oid': user.organization} }