我有2个收藏品。
ItemList = new Mongo.Collection('items');
BorrowerDetails = new Mongo.Collection('borrow');
ItemList.insert({
brand: "brand-Name",
type: "brand-Type",
._id: id
});
BorrowerDetails.insert({
key: "ItemList.id", //equals to .id of the ItemList Collection
name : "borrowerName"
});
问题!
如何根据ItemList Collection中的某种类型从BorrowerDetails集合中检索记录。
离。从BorrowerDetails Collection中检索所有记录,其中key等于ItemList Collection上记录的id,其类型等于" Desktop"。
return BorrowerDetails.find(
{ key :
ItemList.find(
{ type : 'Desktop' },
{ fields: {'_id':1 } }
)
}
); //error!
答案 0 :(得分:0)
请注意,我的笔记本电脑中目前没有nodejs,因此可能会有多处错误,因为我无法测试它。
首先,创建一个发布文件(例如server \ publications \ borrowPub.js)。在文件中,您应该创建一个发布方法。这里的逻辑很简单,首先获取itemid数组,然后将其作为Mongo select $in.
中的参数传递Meteor.publish('queryBorrowerTypeDesktop', function(criteria)
{
var itemArr = ItemList.find( { type : 'Desktop' },
{ fields: {'_id':1 } });
if(itemArr){
//itemArr found, so do a select in using the array of item id we retrieved earlier
var borrowers = BorrowerDetails.find({ key: { $in: itemArr } });
return borrowers;
}else{
//found nothing- so return nothing
return []
}
});
其次,在路由器中:
Router.route('/test', {
name: 'test',
action: function()
{
//change accordingly.
},
waitOn: function()
{//subscribe from publisher that we just created...
return [
Meteor.subscribe('queryBorrowerTypeDesktop')
];
},
data: function() {
if(Meteor.userId())
{
// also include the sorting and limit so your page will not crash. change accordingly.
return {
borrowerDetails: BorrowerDetails.find({},{sort: {name: -1}, limit: 30}),
}
}
}
});
请注意,在数据中,BorrowerDetails.find()不需要过滤任何内容,因为它已在订阅期间进行过滤,已在浏览器的MiniMongo中缓存。