我有两个不同的收藏品集A和收藏B. 集合B具有集合A的主键。
如果集合B中有10条记录,其主键是集合A,那么我想要新的字段countInB,它将是10
我创建了集合A的发布方法,并在集合A方法中获得了该计数。
所以它完美无缺。我参考了https://forums.meteor.com/t/why-is-mongo-aggregation-not-reactive-in-meteor/7462/6
Collection A :
[{"_id":"123456"},{"_id":765456}]
Collection B:
[{"_id":"dfsdfsdf",aid:"123456"},{"_id":"fsfds",aid:"123456"}]
collectionA = new Mongo.Collection('A');
发布方法中的代码:
Meteor.publish("publishMethodOfA",function(){
var sub = this;
sub.ready();
var query = collectionA.find();
var handle = query.observeChanges({
added: function (id) {
//added function
},
removed: function (id) {
//remove method
},
changed: function (id) {
//change method
},
error: function(err){
throw new Meteor.Error('Uh oh! something went wrong!', err.message);
}
});
var pipeline = [
{
$lookup: {
from: "B",
localField: "aid",
foreignField: "_id",
as: "counts"
}
},
{
"$project": {
"_id" : 1,
"counts" : { $size: "$counts" }
}
}
]
collectionA.aggregate(pipeline);
sub.onStop(function () {
handle.stop();
});
});
当集合A发生变化时,该方法完全正常,并且它也会调用,因此没有任何问题
但是当集合B中有任何插入时,那么在这种情况下我想更新A的发布方法中的计数,那我怎么能这样做呢?