简体   繁体   English

流星将JSON导入集合

[英]Meteor Import JSON into Collection

I am trying to import a JSON file that has embedded objects into the MongoDB and would like to know how to format to import the doc correctly. 我正在尝试将具有嵌入对象的JSON文件导入MongoDB,并想知道如何格式化以正确导入doc。 To be clear the doc format must remain in its current form. 为了清楚起见,文档格式必须保持其当前格式。 I must modify/create a function or method that would allow this file to be imported with _id associated in the sport-events array loop (Ive shorted to one sports-event array for brevity). 我必须修改/创建一个函数或方法,该函数或方法将允许使用在Sport-Events数组循环中关联的_id导入此文件(为简便起见,Ive简称为一个Sport-Event数组)。 Perhaps a better way of putting the question is how could I import eveything in the sports-event array and leave the rest of the doc. 提出这个问题的一种更好的方法可能是如何在体育赛事数组中导入eveything,并剩下其余的文档。

JSON Doc. JSON文件

{
"sports-content": {
    "sport-event": [{
        "event-metadata": {
            "league": "NCAA Basketball",
            "event-type": "0",
            "league-details": "NCAAB",
            "event-date-time": "12/18/2015 07:00 PM",
            "eventNum": "3000460",
            "status": "",
            "off-the-board": "False"
        },
        "team": [{
            "team-metadata": {
                "alignment": "Home",
                "nss": "526",
                "openNum": "526",
                "name": {
                    "full": "Clemson"
                }
            },
            "wagering-stats": {
                "wagering-straight-spread": {
                    "bookmaker-name": "BetOnline",
                    "active": "true",
                    "line": "1.5",
                    "money": "-110",
                    "context": "current"
                }
            },
            "team-stats": {
                "score": "0"
            }
        }, {
            "team-metadata": {
                "alignment": "Away",
                "openNum": "525",
                "nss": "525",
                "name": {
                    "full": "South Carolina"
                }
            },
            "wagering-stats": {
                "wagering-straight-spread": {
                    "bookmaker-name": "BetOnline",
                    "active": "true",
                    "line": "-1.5",
                    "money": "-110",
                    "context": "current"
                }
            },
            "team-stats": {
                "score": "0"
            }
        }]
    }],
    "sports-meta-data": {
        "doc-time": "42353.5979256944"
    }
  }
 }

Meteor server.js under Meteor.startup Meteor.startup下的Meteor server.js

if (SportEventsFeed.find().count() === 0) {
    console.log("Importing private/products.json to db")

    var data = JSON.parse(Assets.getText("ncaab.json"));

    data.forEach(function (item, index, array) {
        SportEventsFeed.insert(item);
    })
}

Here is how you can just insert the items from the 'sports-event' array as individual documents in the db. 这是您仅可以将'sports-event'数组中的项目作为单个文档插入db中的方法。
I'm not sure if the index value is what you wanted for the _id field, if not change the value assigned to item_id (or comment out this line for a default _id). 我不确定索引值是否是您想要的_id字段,如果不更改分配给item_id的值(或注释掉此行以获取默认的_id)。

if (SportEventsFeed.find().count() === 0) {
  console.log("Importing private/products.json to db")

  var data = JSON.parse(Assets.getText("ncaab.json"));

  data['sports-content']['sports-event'].forEach(function (item, index, array) {
    item._id = index;
    SportEventsFeed.insert(item);
  })
}

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

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