Reputation: 24314
I need to import data from many json files with mongoose, but I can't seem to find how. Now I'm just importing the files using mongoDB commands:
mongoimport --db dbName --collection collectionName --file fileName.json --jsonArray
Is there a way to do it with mongoose? is it the right way to do it with mongoose (I'm using expressJs)? if not what's the right way to populate my database with data from json files?
This is what I've done so far in server.js
/ Set up
var express = require('express');
var app = express(); // create our app w/ express
var mongoose = require('mongoose'); // mongoose for mongodb
var ObjectId = require('mongodb').ObjectID; // objectID
var morgan = require('morgan'); // log requests to the console (express4)
var bodyParser = require('body-parser'); // pull information from HTML POST (express4)
var methodOverride = require('method-override'); // simulate DELETE and PUT (express4)
var cors = require('cors');
// Configuration
mongoose.connect('mongodb://localhost/data');
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({ 'extended': 'true' })); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(methodOverride());
app.use(cors());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'DELETE, PUT');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// Models
var Review = mongoose.model('Review', {
title: String,
description: String,
rating: Number
});
var Site = mongoose.model('Site', {
title: String,
description: String,
rating: Number
});
// Routes
// Get reviews
app.get('/api/reviews', function(req, res) {
console.log("fetching reviews");
// use mongoose to get all reviews in the database
Review.find(function(err, reviews) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err)
res.json(reviews); // return all reviews in JSON format
});
});
Upvotes: 0
Views: 2343
Reputation: 56
I am having the same issue, Using mongoimport bypasses the schema and makes the schema type checking and validation useless. So, what I did works fine for a single json object. I am planning in extending it to work on the whole json file. So, I have a POST request which adds data to the mongoDB going through schema. I made a curl script to make that same post request and does go through the schema. Here is my curl script.
curl \
--header "Content-type: application/json" \
--request POST \
--data @data.json \
http://localhost:3000/supplierList
I have a json object in the data.json file. Here is how it looks like.
{
"supplierName": "KINGCOBRA",
"price": "12345678911234567.00000000000000001",
"basicDate": "2045/03/23",
"currency": "USD",
"partName": "ASDF",
"description": "First Object: TEST 1: POST Inserting an array of object through the script",
"volume": 2334
}
The only problem with this approach is that it does not support insertation of json array.But it will work for single object regardless of it's size. Let me know if anyone found a workaround for that.
Upvotes: 1