Mongodb aggregate with Meteor objectId

Hi,
Could someone please explain to me (or show an example) of how to use a Meteor idObject in an aggregate to match.
I sounds stupid but i cannot find a answer to this question.
Here is an example of what I have tried without success

archivedCount = db.Projects.aggregate([
{
$match: {
employerId: new Meteor.Collection.ObjectID(employerId),
archived: true,
},
},
{
$group:{
_id: null,
archivedTotal: {$sum: 1}
},
},
])

Thanks in advance for taking the time to help me!

Hi @pierrehop, do you need help with formatting this code so we can look at it?

Hi @paulishca,
Sorry about the delay, I tried to email you but it did not work.
Any help you could give me would be greatly appreciated
Thanks again for reaching out

That doesn’t look like a valid command, Meteor’s Mongo.Collection class doesn’t have aggregate method. You can access native MongoDB Node.js driver’s async aggregate method with await Projects.rawCollection().aggregate([...]).toArray();

What type does employerId have?

I assume it’s a string, but in the actual DB it’s an ObjectId, right?
If so, then you either need to:
import { MongoInternals } from 'meteor/mongo';
and use it:
new MongoInternals.NpmModule.ObjectID(employerId)

or you can install native MongoDB Node.js driver to your app with meteor npm i mongodb@4.15.0, import ObjectId from there:
import { ObjectId } from 'mongodb';
and use it:
new ObjectId(employerId)

@dbar,
Thanks a lot for your help, I am new to this and was having a hard time finding docs on the subject.
I truly appreciate this


// paste your linted javascript code here

@paulishca
Thank you very much for your offer but I manage to get it going.
My biggest issue was to match the objectId from Meteor and dbar got me going with the MongoInternals import.
Thanks again for helping