Cannot use reative aggregation package with ObjectIDs

Hello,
I’m starting with Meteor and I’m enjoying it,

I wanted to use reactive aggregation, so I found this Package: https://github.com/JcBernack/meteor-reactive-aggregate

When using this package, I got ERROR: Meteor does not currently support objects other than ObjectID as ids.

according to this issue this package supports only IDs as Strings and not ObjectIDs.

I did some research and understood that ObjectIDs are better or at least more robust.

My question is (the App is still in the early Dev stage),

1- should I switch to String IDs to fit the plugin ? (which sounds strange to me, the plugin should fit multiple scenarios not the other way around)

2- is there a way when I can use this package and keep the IDs as ObjectIDs ?

or

3- is there another way to use reactive aggregation with ObjectIDS ?

Thank you.

I think you’ve got the message confused.

It actually says “objects other than ObjectIds”. Meteor does support ObjectIds as ids. Maybe you have defined your own object type to use as an _id?

EDIT: Actually, that’s spectacularly easy to do in the aggregation pipeline. If you use $group, for example, then you frequently define a non-ObjectId object for the grouping _id. If that’s where you believe you’re seeing this, you will need to make those _ids strings (or ObjectIds).

Thank you for your quick answer,

indeed the Error message is confusing because my _id is generated like this : const id = new Meteor.Collection.ObjectID();

but the problem seems in the plug-in as it was stated in this issue

I tried to empty my DB and use String IDs, it did work but I want to keep my _id field as ObjectID.

I’m not using $group, actually I’m using aggregation because I want to match my ISODate field to a regex , so I’m using it in this way:
pipe= [
{$project:{ -somefields- ,date : { $dateToString: { format: “%Y-%m-%d”, date: “$lastUpdate” } } }},
{$match:{ date: /searchinput.*/i }},
]

I couldn’t find another way to search through ISODate field (I’m open to a better solution if there is one)

EDIT: I was hoping this works but it doesn’t : $project:{ _id : “$_id._str”,…}.
Is there a way to extract the String from the ObjectID in the $project stage ?

You’re correct when you say that you can’t match a regex in a standard query. However, I’d first ask if a regex is strictly necessary? There can be massive performance hits using regex. Are you able to use ranged date queries instead?

As for _ids in the aggregation pipeline, the $project operator passes the _id through unchanged (unless you’ve specifically excluded it), so I’m still unclear what’s happening in your use case. Perhaps, some more code may help?

I ended up splitting the date on the insert and keep something like this { day , month , year } as strings . This way I can stick with only find() without the need of aggregation.

Thank you very much for your help, much appreciated :slight_smile:

1 Like