我如何从mongodb解析这个json与meteor helper一起使用

时间:2015-07-07 06:48:38

标签: javascript json mongodb meteor

我在集合中有这个json数据,我需要获得first_yr,second_yr_sem_1,second_yr_sem_2的所有fiels /值....

  {
    "first_yr": [
                  {
                   "subject_name": "Subject1",
                   "subject_val": "subject1"
                 },
                 {
                   "subject_name": "Subject2",
                   "subject_val": "subject2"
                 },
                 {
                   "subject_name": "Subject3",
                   "subject_val": "subject3"
                 },
                 {
                   "subject_name": "Subject4",
                   "subject_val": "subject4"
                 },
                 {
                   "subject_name": "Subject5",
                   "subject_val": "subject5"
                 },
                 {
                   "subject_name": "Subject6",
                   "subject_val": "subject6"
                 }
              ]

  },
  {
    "second_yr_sem_1":
              [
                {
                 "subject_name": "Subject1",
                 "subject_val": "subject1"
               },
               {
                 "subject_name": "Subject2",
                 "subject_val": "subject2"
               },
               {
                 "subject_name": "Subject3",
                 "subject_val": "subject3"
               },
               {
                 "subject_name": "Subject4",
                 "subject_val": "subject4"
               },
               {
                 "subject_name": "Subject5",
                 "subject_val": "subject5"
               },
               {
                 "subject_name": "Subject6",
                 "subject_val": "subject6"
               }
              ]

  },
  {
    "second_yr_sem_2":
                [
                  {
                   "subject_name": "Subject1",
                   "subject_val": "subject1"
                 },
                 {
                   "subject_name": "Subject2",
                   "subject_val": "subject2"
                 },
                 {
                   "subject_name": "Subject3",
                   "subject_val": "subject3"
                 },
                 {
                   "subject_name": "Subject4",
                   "subject_val": "subject4"
                 },
                 {
                   "subject_name": "Subject5",
                   "subject_val": "subject5"
                 },
                 {
                   "subject_name": "Subject6",
                   "subject_val": "subject6"
                 }
              ]

  }

与此流星助手一起使用

<select name="subject">
         {{#each subjects}}
         <option value="{{subject_val}}">{{subject_name}}</option>
         {{/each}}
</select>

那么,我应该回到帮助者(主题)的mongodb查询应该是什么? 我卡住了,什么都可以帮助我! 谢谢!

3 个答案:

答案 0 :(得分:1)

对不起,我不知道你的意思。所以我想你将Json直接保存在你的集合中,我认为你可以使用 cursor.forEach 将每个字符串解析为 template.helpers中的对象:

Template["myTemplate"].helpers({
  var subjects = [];
  var myDataCursor = MyCollection.find();
  myDataCursor.forEach(function(eachSubject) {
    subjects.push(JSON.parse(eachSubject));  // parse str to object, and save it in an array.
  });
  return subjects;
})

您可以找到更多详细信息here

我希望这可以完美地解决您的问题: - )

答案 1 :(得分:0)

要从mongoDB获取所有数据,您需要使用内置的“.find”方法。 例如:

app.get('/user/all', function(req, res) {

     // use mongoose to get all users in the database
    User.find(function(err, user) 
    {
            // if there is an error retrieving, send the err 
    if (err)
    {
        res.send(err);
    }

    // return all users in JSON format
    res.json(user);

});
});

用户是MongoDB中架构的名称。您的所有数据当前都不在“用户”中。您将需要解析此数据,因为您的JSON是一个对象数组,您使用for循环进行解析,然后将值传递给前面的变量。

我没有那么多使用流星,所以我很确定。 我希望这会有所帮助。

答案 2 :(得分:0)

Template.myTemplate.helpers({ subjects: function() { return YourCollection.find(); } });