-
Notifications
You must be signed in to change notification settings - Fork 0
/
quesBankMongoose.js
47 lines (36 loc) · 1.05 KB
/
quesBankMongoose.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
fs = require('fs');
fs.readFile('questions2.json', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
var json=JSON.parse(data);
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/quizRT');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function (callback) {
console.log('connection open');
var quesBankSchema = mongoose.Schema({
questionId: {type:String, unique:true},
image: String,
question: String,
correctIndex: Number,
option1: String,
option2: String,
option3: String,
option4: String,
topicId: String
},{strict:false});
var quesBank = mongoose.model('quesBank', quesBankSchema, "question_bank_collection");
for(i=0;i<json.length;++i)
{
var ques = new quesBank(json[i]);
ques.save(function(err){
if ( err ) console.log(err);
console.log("Ques "+i+" Saved Successfully");
});
}
console.log('closing mongo');
//mongoose.disconnect();
});//end once
});//end readfile