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

here is my document so how can i store ??? enter image description here. i'm able to write REST Api only for one document but not for multiple document . I'm invoking service using POSTMAN.

Cisco
  • 20,972
  • 5
  • 38
  • 60

2 Answers2

1

You can create schema like this:

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

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

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

You can use this example to insert multiple records in one time.

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)
});
harpreet cheema
  • 165
  • 1
  • 6
  • thanks for ur suggestion .. but I'm little bit confuse how to make schema ?? because i'm using mongoose so to insert any document first , we need to make a appropriate schema just like our json structure – mohit choudhary Jan 30 '18 at 08:14
  • thanks @harpreet but what if we want to take var obj=[{ email:"test@gmail.com" }, { email:"test1@gmail.com" }, { email:"test2@gmail.com" }]; , this json data with POSTMAN , then how we can set all the data on a object ? – mohit choudhary Jan 30 '18 at 08:57
  • can we do like this var schemaName = new SchemaName ({ email:req.body.email }); CollectionName.insertMany(" obj ???not sure ") .then(function(res) { console.log("=============insert===", res) }) – mohit choudhary Jan 30 '18 at 09:04
0

so you want to inserty multiple documents at a time.

in mongodb there is some limit of documet at a time you can save. it can support upto 16MB of BSON Document Size. see it here.

now lets talk about insertMany documents at at a time.

this following query inserts multiple dicuments at a time:

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

now insted of [ <document 1> , <document 2>, ... ] you can pass your document. in Array of Objects format (as you given).

Saikat Chakrabortty
  • 2,520
  • 4
  • 22
  • 39
  • thanks saikat .. but how i can bind my json data in mongoose Schema ? give me some suggestion about that because my problem is about schema and to invoke service. – mohit choudhary Jan 30 '18 at 06:02
  • as per my understanding, you have an express app, and a route, and already database connection is done, now you want to give a post request with your data and save it. am i right? if you need only the schema, i will update in the same answer. – Saikat Chakrabortty Jan 30 '18 at 06:08
  • yes u r right .. let's take a scenario , DB Connection is already done .we have all the data in postman body and one POST request , for this json we need a Mongoose Schema that will looks like – mohit choudhary Jan 30 '18 at 06:15
  • const mongoose = require('mongoose'); var dataVisulations= mongoose.Schema({ _id:mongoose.Schema.Types.ObjectId, end_year:String,intensity:Number, sector:String,topic:String,insight:String, url:String, region:String,start_year:String, impact:String, added:String, published:String, country:String, relevance:Number, pestle:String, source:String, title:String, likelihood:Number }); module.exports=mongoose.model('DataVisulation',dataVisulations); – mohit choudhary Jan 30 '18 at 06:19
  • and now we have a post request router.post('/',(req , res , next)=>{var dataVisulation = new DataVisulation({ end_year:req.body.end_year,........and so on for all the key ..}) – mohit choudhary Jan 30 '18 at 06:20
  • now ??? I'm facing problem here .. if i have only one document then i can do like. . dataVisulation.save().then( res.status(201).json({message:'data inserted ')} – mohit choudhary Jan 30 '18 at 06:25
  • thats what i wrote, insted of doing all this things, just `collectionaname.insertMany(ArrOfUrDoc,{ ordered: });` – Saikat Chakrabortty Jan 30 '18 at 06:48