gpt4 book ai didi

arrays - Meteor、MongoDB通过订阅获取数组的一部分

转载 作者:可可西里 更新时间:2023-11-01 09:23:11 25 4
gpt4 key购买 nike

我有一个关于如何使用 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"
}]

最佳答案

看起来您只是在“userBankAdvanced”发布函数中缺少“fields”说明符。我写了一个test in meteorpad using your example它似乎工作正常。为简单起见,银行 ID 在此处进行了硬编码。

所以代替

return Meteor.users.find({_id:this.userId,"bankList.id": bankId}, {'bankList.$': 1});

尝试使用

return Meteor.users.find({_id:this.userId,"bankList.id": bankId}, {fields: {'bankList.$': 1}});

关于arrays - Meteor、MongoDB通过订阅获取数组的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25349873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com