Meteor将JSON导入Collection

时间:2015-12-21 22:01:17

标签: javascript json mongodb meteor

我正在尝试将具有嵌入对象的JSON文件导入MongoDB,并且想知道如何格式化以正确导入文档。要明确的是,doc格式必须保持其当前形式。我必须修改/创建一个函数或方法,允许使用在运动事件数组循环中关联的_id导入此文件(为简洁起见,我已短接到一个运动事件数组)。也许提出问题的一个更好的方法是如何在体育事件数组中导入eveything并留下文档的其余部分。

JSON Doc。

{
"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.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);
    })
}

1 个答案:

答案 0 :(得分:1)

以下是如何插入体育赛事'数组作为数据库中的单个文档 我不确定索引值是否是_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);
  })
}