MongoDB Node.js从现有数据中提取测试数据

时间:2019-03-12 05:09:40

标签: javascript node.js mongodb

这是我在generate.js文件中的代码。我正在尝试从现有数据数组生成100k数据。但是有一个错误:BulkWriteError:E11000重复键错误集合。有人有建议吗?

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

var myobj1 = [
{ "Year" : 1998, "Cause Name" : "Suicide", "State" : "United States", "Deaths" : 31655, "Age-adjusted Death Rate" : 11, "Date Inserted" : new Date('1998-08-25T00:00:00Z') },
{ "Year" : 1999, "Cause Name" : "All causes", "State" : "United States", "Deaths" : 2319475, "Age-adjusted Death Rate" : 616, "Date Inserted" : new Date('1999-07-05T00:00:00Z') },
{ "Year" : 2000, "Cause Name" : "All causes", "State" : "Alaska", "Deaths" : 2914, "Age-adjusted Death Rate" : 869.1, "Date Inserted" : new Date('2000-08-14T00:00:00Z') },
{ "Year" : 1996, "Cause Name" : "All causes", "State" : "Alabama", "Deaths" : 44806, "Age-adjusted Death Rate" : 1009.3, "Date Inserted" : new Date('1996-10-03T00:00:00Z') },
{ "Year" : 1997, "Cause Name" : "Stroke", "State" : "Georgia", "Deaths" : 4416, "Age-adjusted Death Rate" : 72.3, "Date Inserted" : new Date('1997-06-16T00:00:00Z') }

for (var i = 0; i < 50; i++){
	myobj1 = myobj1.concat(myobj1);
}

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("DatabaseSystem");
  
  console.log(args);
  
  dbo.collection("death").insertMany(myobj1, function(err, res) {
	if (err) throw err;
	console.log("Number of documents inserted: " + res.insertedCount);
	db.close();
  });
});

0 个答案:

没有答案