简体   繁体   English

我如何从mongodb解析此json以与流星助手一起使用

[英]How can i parse this json from mongodb to use with meteor helper

i have this json data in a collection and i need to get all the fiels/values of first_yr, second_yr_sem_1 , second_yr_sem_2 .... 我在集合中有此json数据,我需要获取first_yr,second_yr_sem_1,second_yr_sem_2 ...的所有字段/值。

  {
    "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"
                 }
              ]

  }

to use with this meteor helper 与此流星助手一起使用

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

So, what should be the mongodb query i should return to the helper(subjects) ?! 那么,应该返回给helper(subjects)的mongodb查询应该是什么? Im stuck,anything could help me! 我被困住了,有什么可以帮助我的! Thanks!! 谢谢!!

Sorry, I don't know what you mean exactly. 抱歉,我不知道您的意思是什么。 So I suppose that you save the Json directly in your collection, and I think you could use cursor.forEach to parse each string to object in your template.helpers: 因此,我想您将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;
})

You can find more detail here 你可以在这里找到更多细节

I wish this can solve your problem perfectly :-) 我希望这可以完美解决您的问题:-)

To get all data back from mongoDB you need use the in-built ".find" method. 要从mongoDB中取回所有数据,您需要使用内置的“ .find”方法。 For example: 例如:

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);

});
});

User is the name of your schema in MongoDB. 用户是您在MongoDB中的架构名称。 All your data is not currently in "user". 您的所有数据当前不在“用户”中。 You will need to parse this data, as your JSON is an array of objects you cause a use a for-loop to parse and then pass the values to your variables on the front it. 您将需要解析此数据,因为JSON是对象数组,因此会导致使用for循环进行解析,然后将值传递给位于其最前面的变量。

I haven't used meteor that much so I am sure about that. 我没用太多流星,所以我对此很确定。 I hope this helps. 我希望这有帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM