我今天试图在我的macbook上导入一个Json文件,但是我收到了一个错误:
Davids-MacBook-Pro:server Droberts$ node import.js
/filepath/A_to_B.json
development
mongodb://localhost/somefoldername
{ db: { safe: true } }
A
B
/Users/Droberts/Desktop/folderA/folderB/node_modules/mongoose/node_modules/mongodb/lib/server.js:236
process.nextTick(function() { throw err; })
^
Error: connect ECONNREFUSED
at exports._errnoException (util.js:746:11)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)
但是,在新标签页中运行mongod
后,我能够成功重新运行该命令:
Davids-MacBook-Pro:server Droberts$ node import.js
/filepath/A_to_B.json
development
mongodb://localhost/somefoldername
{ db: { safe: true } }
A
B
adding data to mongodb
finished import
然而,最令人吃惊的是,我的数据库仍然是空的!:
Davids-MacBook-Pro:server Droberts$ mongo
MongoDB shell version: 3.2.1
connecting to: test
Server has startup warnings:
2016-01-15T17:50:09.158-0500 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2016-01-15T17:50:09.158-0500 I CONTROL [initandlisten]
2016-01-15T17:50:09.158-0500 I CONTROL [initandlisten]
2016-01-15T17:50:09.158-0500 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
> show dbs
somefoldername 0.000GB
local 0.000GB
到底是怎么回事?!!!以下是node.js
的来源:
'use strict';
// Set default node environment to development
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
console.log(process.env.NODE_ENV);
var mongoose = require('mongoose');
var config = require('./config/environment');
var CityPair = require('./api/citypair/citypair.model');
console.log(config.mongo.uri)
console.log(config.mongo.options)
// Connect to database
mongoose.connect(config.mongo.uri, config.mongo.options);
var fs = require('fs');
var file = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
console.log(file.startCity.name)
console.log(file.endCity.name)
//CityPair.find({}).remove(
CityPair.find({
$or: [{startCity : file.startCity, endCity : file.endCity},
{startCity : file.endCity, endCity : file.startCity}]
}).remove(
function() {
console.log('adding data to mongodb');
CityPair.create(file, function(err){
console.log('finished import');
process.exit();
});
}
);