Hi!
I have a really inefficient subscription because of several nested finds
in a publishComposite
.
I think this can be much more performant if converted to an Aggregate
and using $lookups
.
However, a regular Aggregate
will only return a Cursor to the main collection it’s being called on. I will not have Reactivity for my “joined” collections in MiniMongo (rather it will just exist as data in my $project
stage)
Is there anyway to get cursors for my $lookup
collections too?
Update: I’m using tunguska-reactive-aggregate
and it looks like it may be possible in this and in the JcBernack original.
I have a basic example below. I
Client
Meteor.subscribe('test', parentId);
Server
Meteor.publish('test', function (parentId) {
const pipeline = [
{
'$match': {
'_id': parentId
}
}, {
'$lookup': {
'from': 'Children',
'localField': '_id',
'foreignField': 'parentId',
'as': 'CHILDREN'
}
}
];
ReactiveAggregate(this, Parents, pipeline, {
noAutomaticObserver: true,
observers: [
Parents.find({_id: parentId}),
Children.find({parentId: parentId})
]
});
});
On the client I see the main Parent
doc with {_id: parentId}
but I still don’t get my Children
docs sent over. I don’t see the cursor in the MiniMongo Explorer either. I do however see the ‘CHILDREN’ property on my main Parent Doc that gets sent over.
I’ve also tried
ReactiveAggregate(this, Parents, pipeline, {
lookupCollections: {
Children:{
observeSelector: {parentId: parentId}
}
}
});
as per the original JcBernack instructions but that didn’t work either.
Edit: If I pass in the Collection Object itself into from
in the $lookup
instead of String 'ChildrenI get "
Exception from sub test id Ai9u5eFfJ8SXhKR7r Error: cyclic dependency detected`"
I’m using SimpleSchema in both Collections and am using the Meteor default of String for _ids
I passed in debug:true
and have verified that the observers are being initialized and that the pipeline gets ran. Upon debugging, error gets thrown while trying to fetch
doc in a toArray
. It’s an error while attempting to sanitize the doc. Then - the observers get stopped
Any insights?
Thanks!