meteor js遍历mongodb游标

时间:2016-08-05 16:43:35

标签: mongodb meteor

我在用户集合中有这个结构。

user: _id: "****",
      username: "username",
      { profile: 
               "name", "name",
               "members": [
                        {"name": "name1", "level":"level1"},
                        {"name": "name2", "level":"level2"},
                        {"name": "name3", "level":"level3"}
                          ]
      }

我无法弄清楚如何将成员数组放在javascrit数组中。 我想以这种格式获得数组:

         [{"name": "name1", "level":"level1"}, 
          {"name": "name2","level":"level2"},
          {"name": "name3", "level":"level3"}
         ]

我尝试过的事情:

Template.mytemplate.helpers({
 members : function(doc){
    var members = [];
    var result= [];
    members = Users.find({"_id": Meteor.userId()}, 
                         {fields :{"profile.memebers": 1 } });
    members.forEach(function(doc)
     {
      result.push({"name": "doc.members.name", "level": doc.member.level});
    });
   return result;
 });

1 个答案:

答案 0 :(得分:1)

用户:

_id: "****",
      username: "username",
      { 
         profile: {
               "members": {"name": "name1", "level":"level1"}
                  }
  }

我认为这应该是你收藏中的正确结构。

Template.mytemplate.helpers({
 members : function(){
    var members = [];
    var result= [];
    members = Users.find({"_id": Meteor.userId()}, 
                         {fields :{"profile.members": 1 } });
    members.forEach(function(doc)
     {
      result.push({name: doc.profile.members.name, level: doc.profile.members.level});
    });
   return result;
 });

然后这是帮手

<template name="mytemplate">
<h2>User List</h2>
{{#each members}}
<ul>
<li>{{name}}</li>
<li>{{level}}</li>
</ul>
{{/each}}
</template>