我有一个关于如何只得到使用的MongoDB和MeteorJS数组的某一个元素的问题。我对用户文档以下模式:
bankList: { ID:34567890987654345678 名称:xfgchjbkn 类型:信用 }, { ID:09876543456789098767 名称:65789876t8 类型:借记 } ]我第一次订阅只有数组中的字段的一部分,特别是我收集所有的ID列表。然后,我有一个编辑屏幕,应该订阅所有字段与匹配的ID阵列中的一个特定元素。我不想暴露阵列仅仅单个元件的其他部分。目前,我使用下面的方法首先收集只是id的列表:
Meteor.users.find({_ ID:this.userId}, {字段:{'bankList.id':1}});和下面的出版物订阅方法得到的只是一个特定元素的信息:
出版:
Meteor.publish(userBankAdvanced功能(bankId){ 检查(bankId,字符串); 如果(this.userId){ 返回Meteor.users.find({_ ID:this.userId,bankList.id:bankId},{'bankList $':1}); }其他{ this.ready(); } });订阅:
this.route('edit_account',{ 路径:'/编辑/帐号/', waitOn:功能(){ 如果(Session.get(bankId)){ 返回Meteor.subscribe('userBankAdvanced',Session.get(bankId)); } 返回null; }, 数据:功能(){ 如果(Session.get(bankId)){ 返回Meteor.users.findOne(); } 返回null; }, onBeforeAction:功能(){ beforeHooks.isRevise(Session.get(bankId)); } });订阅方法返回所有与所有的信息数组的元素。我想,例如,仅这(而不是整个所有的信息列表):
bankList: { ID:34567890987654345678 名称:xfgchjbkn 类型:信用 }]解决方案
看起来你只是缺少你的userBankAdvanced发布功能田说明。我写了一个试验meteorpad使用你的榜样,它似乎很好地工作。银行id是简单有硬codeD。
所以不是
返回Meteor.users.find({_ ID:this.userId,bankList.id:bankId},{'bankList $':1});请尝试使用
返回Meteor.users.find({_ ID:this.userId,bankList.id:bankId},{字段:{'bankList $':1}});I have a question about how to just get a certain element of an array using MongoDB and MeteorJS. I have the following schema for the user document:
bankList:[ { id: "34567890987654345678", name: "xfgchjbkn", type: "credit" }, { id: "09876543456789098767" name: "65789876t8", type: "debit" } ]I first subscribe to only part of the fields in the array, specifically I gather a list of all the ids. Then I have an edit screen that should subscribe to all of the fields for a specific element in the array with a matching id. I do not want to expose the rest of the array just the single element. Currently, I use the following to first gather a list of just the ids:
Meteor.users.find({_id: this.userId}, {fields:{'bankList.id': 1}});And the following publication-subscription method to get just a specific element's information:
Publication:
Meteor.publish("userBankAdvanced", function(bankId){ check(bankId,String); if(this.userId){ return Meteor.users.find({_id:this.userId,"bankList.id": bankId}, {'bankList.$': 1}); }else{ this.ready(); } });Subscription:
this.route('edit_account', { path: '/edit/account/', waitOn: function(){ if(Session.get("bankId")){ return Meteor.subscribe('userBankAdvanced',Session.get("bankId")); } return null; }, data: function(){ if(Session.get("bankId")){ return Meteor.users.findOne(); } return null; }, onBeforeAction: function(){ beforeHooks.isRevise(Session.get("bankId")); } });The subscription method returns all of the elements of the array with all of the information. I want, for example, just this (not the entire list with all of the information):
bankList:[ { id: "34567890987654345678", name: "xfgchjbkn", type: "credit" }]解决方案
It looks like you're just missing the "fields" specifier in your "userBankAdvanced" publish function. I wrote a test in meteorpad using your example and it seems to work fine. The bank id is hardcoded for simplicity there.
So instead of
return Meteor.users.find({_id:this.userId,"bankList.id": bankId}, {'bankList.$': 1});try using
return Meteor.users.find({_id:this.userId,"bankList.id": bankId}, {fields: {'bankList.$': 1}});
更多推荐
流星,MongoDB的打通认购阵列的一部分
发布评论