我有一个关于如何使用 MongoDB 和 MeteorJS 获取数组的某个元素的问题。我的用户文档有以下架构:
bankList:[
{
id: "34567890987654345678",
name: "xfgchjbkn",
type: "credit"
},
{
id: "09876543456789098767"
name: "65789876t8",
type: "debit"
}
]
我首先只订阅数组中的部分字段,特别是收集所有 id 的列表。然后我有一个编辑屏幕,它应该订阅数组中具有匹配 id 的特定元素的所有字段。我不想只公开数组的其余部分的单个元素。目前,我使用以下命令首先收集仅包含 id 的列表:
Meteor.users.find({_id: this.userId},
{fields:{'bankList.id': 1}});
以及以下发布订阅方法来获取特定元素的信息:
出版物:
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();
}
});
订阅:
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"));
}
});
订阅方法返回数组的所有元素以及所有信息。例如,我想要的只是这个(而不是包含所有信息的整个列表):
bankList:[
{
id: "34567890987654345678",
name: "xfgchjbkn",
type: "credit"
}]