Javascript Meteor,基于从另一个集合检索的ID查询集合

Javascript Meteor,基于从另一个集合检索的ID查询集合,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,我有两个收藏 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

我有两个收藏

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集合中的特定类型从BorrowerDetails集合中检索记录

例如,从BorrowerDetails集合中检索所有记录,其中key等于ItemList集合中类型等于“Desktop”的记录的id


请注意,我的笔记本电脑中目前没有nodejs,因此可能会出现一些错误,因为我无法测试它

首先,创建一个发布文件(例如server\publications\borrowPub.js)。在文件内部,您应该创建一个发布方法。这里的逻辑很简单,首先获取itemid数组,然后将其作为参数传入

第二,在路由器中:

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中。

使用NOSQL数据库时,不应考虑加入。先生,您可以简单地解释一下吗?:)好的,给我一分钟…----谢谢先生------先生var借款人中的数量是多少=借款详情。查找({qty:{$in:itemArr})
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}),
  }
  }
 }
});