问题描述
我是 mongo 和 node 的新手,我正在尝试创建一个脚本,它的工作原理基本上类似于 rails rake db:seed
任务,其中我有一组特定的 .json 文件文件夹,当我运行 $ node seed
时,它将运行我的 seed.js 文件,该文件获取所有这些 .json 文件,并根据 .json 文件的文件名将内容插入到相应的 mongo 集合中.
I'm new to mongo and node and i'm trying to create a script that essentially works similar to rails rake db:seed
task where I have a set of .json files in a specific folder, and when I run $ node seed
it will run my seed.js file which takes all of those .json files and inserts the contents into their appropriate mongo collections based on the filename of the .json file.
本质上我运行 $ node seed
并执行 /seed.js
,它查看 /seeds/account.json
,/seeds/customer.json
等
Essentially I run $ node seed
and it executes /seed.js
which looks at /seeds/account.json
, /seeds/customer.json
, etc.
这样我就可以提交这段代码,然后下一个开发人员可以运行 $ node seed
并且他的 mongo 实例将填充一些数据来处理.
This way I can commit this code and then next developer can just run $ node seed
and his mongo instance will be populated with some data to work with.
我遇到的问题是,我得到的数据来自我在 mongo 中的集合的转储,因此帐户的一些示例数据如下所示:
The problem i'm having is that the data that I got is from a dump of my collections in mongo so some sample data for account looks like this:
{ _id: { '$oid': '534d832177da4e0d38000001' },
id: 1,
name: something,
dateTime: '2014-04-15T14:06:09-05:00' }
现在,当我运行 $ node seed
时,出现此错误:
Now when I run $ node seed
, I get this error:
[Error: key $oid must not start with '$']
有什么办法可以解决这个错误,还是我必须从我所有的 .json 文件中删除所有 $oid
?
Is there any way to get around this error or am I going to have to remove all the $oid
's from all my .json files?
seed.js:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/testdb');
var dir = './seeds';
var db = mongoose.connection;
// Show connection error if there is one
db.on('error', console.error.bind(console, 'Database Connection Error:'));
// If we successfully connected to mongo
db.once('open', function callback() {
var fs = require('fs'); // Used to get all the files in a directory
// Read all the files in the folder
fs.readdir(dir, function(err, list) {
// Log the error if something went wrong
if(err) {
console.log('Error: '+err);
}
// For every file in the list
list.forEach(function(file) {
// Set the filename without the extension to the variable collection_name
var collection_name = file.split(".")[0];
var parsedJSON = require(dir + '/' + file);
for(var i = 0; i < parsedJSON.length; i++) {
// Counts the number of records in the collection
db.collection('cohort').count(function(err, count) {
if(err) {
console.log(err);
}
});
db.collection(collection_name).insert(parsedJSON[i], function(err, records) {
if(err) {
console.log(err);
}
console.log(records[0]);
console.log("Record added as "+records[0]);
});
}
});
});
});
或者是否有已经存在用于此类任务的库?
Or is there any libraries out there that already exist for this kind of task?
推荐答案
http://docs.mongodb.org/manual/core/document/#field-names
字段名称不能以美元符号 ($) 字符开头.
The field names cannot start with the dollar sign ($) character.
mongodb 规范的一部分,因为它使用 $ 来表示 $inc 或 $unset 等操作.
Part of the mongodb spec as it uses $ to indicate operations like $inc or $unset.
这篇关于如何使用节点将记录插入到 mongo 中,其中记录具有 $oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!