i’m trying to use Mongo’s aggregate functions in Meteor but it seems not to work… I’ve installed the aggregate package (GitHub - meteorhacks/meteor-aggregate: Proper MongoDB aggregations support for Meteor) and i’m calling it from the server side but still no luck…
This is my error message:
Exception while invoking method 'findMyTodos' TypeError: todos_collection_1.Todos.aggregate is not a function
I’m using the MongoObservable package so maybe thats the problem?
I have this working now by switching to Mongo.Collection instead of MongoObservable.Collection
However since I have to do the query from the server side it seems to be returning bson objectIDs that look like this:
_id : Object
_bsontype : "ObjectID"
id : Uint8Array[12]
Is there a way to get it to return normal hex objectIDs?
Or is there a way to convert this to a hex objectID?
I have my mongo collection set to ‘MONGO’ id generation, i.e:
export const Todos = new Mongo.Collection<Todo>('todos', {idGeneration: 'MONGO'});
If I remove the idGeneration parameter I get back normal strings (not in an ObjectID) but I need to keep the idGeneration (for other areas in my app)
Talking to myself here… but I found a solution.
You can convert the bson id (which contains Uint8Array) to a hex string with:
Buffer.from(uint8).toString(‘hex’)
I’d still like to know why I was getting bson object ids anyway, is that normal behaviour when querying form the server side?
No - you get whatever the _id
is defined as, but as you’re using MongoDB aggregation you get to define the _id
with the $group
operator. If you’re not using $group
, or you’re using null
as the _id
in $group
you probably get the default MongoDB objectId
form.
Did anyone ever find a solution for this?
I had the same issue, which was resolved using Buffer. Seems like Meteor Aggregate returns a different object for ObjectID
import Buffer from 'buffer';
Buffer.Buffer.from(myObject._id.id).toString('hex');
Hi @ruloweb could you explain how you were able to fix this? I cant convert the ObjectId in aggregate.