0
[
        {
            "end_year": "",
            "intensity": 6,
            "sector": "Energy",
            "topic": "gas",
            "insight": "Annual Energy Outlook",
            "url": "http://www.eia.gov/outlooks/aeo/pdf/0383(2017).pdf",
            "region": "Northern America",
            "start_year": "",
            "impact": "",
            "added": "January, 20 2017 03:51:25",
            "published": "January, 09 2017 00:00:00",
            "country": "United States of America",
            "relevance": 2,
            "pestle": "Industries",
            "source": "EIA",
            "title": "U.S. natural gas consumption is expected to increase during much of the projection period.",
            "likelihood": 3
        },
        {
            "end_year": "",
            "intensity": 6,
            "sector": "Energy",
            "topic": "oil",
            "insight": "Annual Energy Outlook",
            "url": "http://www.eia.gov/outlooks/aeo/pdf/0383(2017).pdf",
            "region": "Northern America",
            "start_year": "",
            "impact": "",
            "added": "January, 20 2017 03:51:24",
            "published": "January, 09 2017 00:00:00",
            "country": "United States of America",
            "relevance": 2,
            "pestle": "Industries",
            "source": "EIA",
            "title": "Reference case U.S. crude oil production is projected to recover from recent declines.",
            "likelihood": 3
        }
]

这是我的文件,我该如何存储???在此处输入图像描述。我只能为一个文档编写 REST Api,但不能为多个文档编写。我正在使用 POSTMAN 调用服务。

4

2 回答 2

1

您可以像这样创建架构:

var Mongoose = require('mongoose'),
    Schema = Mongoose.Schema;

var SchemaName = new Schema({
    email: { type: String }
});

var CollectionName = Mongoose.model('CollectionName', SchemaName);

您可以使用此示例一次插入多条记录。

var obj=[{
    email:"test@gmail.com"
},
{
    email:"test1@gmail.com"
},
{
    email:"test2@gmail.com"
}];

CollectionName.insertMany(obj)
.then(function(res) {
    console.log("=============insert===", res)
})
.
catch(function(err){
    console.log("============err nins===", err)
});
于 2018-01-30T08:09:06.863 回答
0

所以你想一次插入多个文档。

在 mongodb 中,一次可以保存的文档有一些限制。它可以支持高达 16MB 的 BSON 文档大小。在这里看到

现在让我们一次讨论insertMany 个文档。

以下查询一次插入多个 dicuments

db.CollectionName.insertMany(
   [ <document 1> , <document 2>, ... ],
   {
      ordered: <boolean[Default True]>
   }
);

现在 [ <document 1> , <document 2>, ... ]你可以通过你的文件了。以对象数组格式(如您所给)。

于 2018-01-30T05:30:31.643 回答