我想在每次迭代中创建一个新文档,而不是AS-IS,文件都在for循环完成所有迭代后存储。我必须在for循环中等待58.000个项目,直到它保存到数据库...
是不是可以告诉mongoose以某种方式在for循环的每次迭代后保存每个文档?
我在代码中引用的mongoose模式称为:PartCluster
const mongoose = require("mongoose");
const fuzzball = require("fuzzball");
const CompanyPart = require("../../models/parts");
const PartCluster = require("../../models/PartClusters");
mongoose.connect("mongodb://localhost:27017/autoMDM", {
keepAlive: true,
reconnectTries: Number.MAX_VALUE,
useMongoClient: true
});
mongoose.Promise = global.Promise;
mongoose.connection.on("error", err => {
console.error(`Mongoose connection error: ${err}`);
process.exit(1);
});
CompanyPart.find({}, { partName: 1 }, (err, parts) => {
var obj = {};
for (var i = 0, len = parts.length; i < len; i++) {
obj[parts[i]["partName"]] = parts[i];
}
parts = new Array();
for (var key in obj) {
parts.push(obj[key]);
}
createCluster(parts);
});
function createCluster(arrayOfParts) {
let usedStrings = [];
let clusterArray = [];
len = arrayOfParts.length;
for (var i = 0; i < arrayOfParts.length; i++) {
let cluster = [];
y = 1;
if (usedStrings.includes(arrayOfParts[i].partName)) {
continue;
}
console.log(`item: ${i}, partName: ${arrayOfParts[i].partName}`);
for (var j = y; j < arrayOfParts.length; j++) {
if (
fuzzball.token_sort_ratio(
arrayOfParts[i].partName,
arrayOfParts[j].partName
) > "70"
) {
if (
arrayOfParts[i].partName.toLowerCase() !==
arrayOfParts[j].partName.toLowerCase() &&
!cluster.includes(arrayOfParts[j].partName) &&
!usedStrings.includes(arrayOfParts[j].partName)
) {
cluster.push(arrayOfParts[j].partName);
usedStrings.push(arrayOfParts[j].partName);
}
}
}
if (cluster.length > 0) {
let obj = {};
obj.clusterName = arrayOfParts[i].partName;
obj.partCluster = cluster.sort();
const query = { clusterName: arrayOfParts[i].partName };
const update = obj;
const options = { upsert: true };
PartCluster.findOneAndUpdate(query, update, options, (error, result) => {
if (error) {
console.log("Error when saving cluster: ", error);
}
});
}
}
}
部分的示例文档
const parts = [
{"_id":"59a942a8c0b7467bf08711df","partName":"0"},
{"_id":"59a94299c0b7467bf084a917","partName":"9129"},
{"_id":"59a94299c0b7467bf084a918","partName":"9130"},
{"_id":"59a94297c0b7467bf08455e0","partName":"9132"},
{"_id":"59a94298c0b7467bf084701f","partName":"9176"},
{"_id":"59a94298c0b7467bf084809f","partName":"9180"},
{"_id":"59a94295c0b7467bf083edf3","partName":"9187"},
{"_id":"59a94298c0b7467bf0848439","partName":"9190"},
{"_id":"59a94295c0b7467bf083ed35","partName":"9227"},
{"_id":"59a94295c0b7467bf083ed36","partName":"9235"},
{"_id":"59a94298c0b7467bf0847032","partName":"9237"},
{"_id":"59a94298c0b7467bf0848438","partName":"9257"},
{"_id":"59a94298c0b7467bf0848c39","partName":"9279"},
{"_id":"59a94295c0b7467bf083edae","partName":"9424"},
{"_id":"59a94295c0b7467bf083edb3","partName":"9425"},
{"_id":"59a9429ec0b7467bf08563ec","partName":"9430"},
{"_id":"59a9429ec0b7467bf08563f6","partName":"9435"},
{"_id":"59a94295c0b7467bf083edb9","partName":"9542"},
{"_id":"59a94295c0b7467bf083edc4","partName":"9577"},
{"_id":"59a942a9c0b7467bf0871bc0","partName":"2651513067"},
{"_id":"59a942aac0b7467bf0874ba5","partName":"COVER"},
{"_id":"59a942aac0b7467bf0874923","partName":"BRACKET"},
{"_id":"59a942a7c0b7467bf086e714","partName":"RING-SNAP"},
{"_id":"59a942aac0b7467bf087443e","partName":"RETAINER"},
{"_id":"59a942aac0b7467bf08749d4","partName":"SPRING"},
{"_id":"59a942aac0b7467bf08748c8","partName":"HOUSING"},
{"_id":"59a942a7c0b7467bf086eaef","partName":"WAGNER PART"},
{"_id":"59a942aac0b7467bf0874a4f","partName":"RING"},
{"_id":"59a942aac0b7467bf0874aef","partName":"GEAR"},
{"_id":"59a942a8c0b7467bf086f701","partName":"HEAD"},
{"_id":"59a942aac0b7467bf08741f2","partName":"WIPER"},
{"_id":"59a942aac0b7467bf08749ea","partName":"SEAL"},
{"_id":"59a94293c0b7467bf08351d6","partName":"PISTON SYS"},
{"_id":"59a942a7c0b7467bf086eac5","partName":"O-RING-VITON"},
{"_id":"59a942aac0b7467bf08749cb","partName":"SHIM"},
{"_id":"59a942a6c0b7467bf086aa6b","partName":"CAGE"},
{"_id":"59a942aac0b7467bf0873e29","partName":"ROD"},
{"_id":"59a942aac0b7467bf0874a30","partName":"ELEMENT"},
{"_id":"59a942aac0b7467bf0874bb6","partName":"PLATE"},
{"_id":"59a942aac0b7467bf087498e","partName":"DIAPHRAGM"},
{"_id":"59a94293c0b7467bf08351e1","partName":"SUPPORT -UPPER"},
{"_id":"59a942aac0b7467bf0874a76","partName":"VALVE"},
{"_id":"59a942aac0b7467bf0873fe5","partName":"LOCK"},
{"_id":"59a94293c0b7467bf08351e7","partName":"FLANG-SPLIT"},
{"_id":"59a942a9c0b7467bf0872e56","partName":"LOCKWASHER"},
{"_id":"59a942aac0b7467bf087425c","partName":"CAPSCREW"},
{"_id":"59a942aac0b7467bf0874b4d","partName":"HOSE"},
{"_id":"59a942aac0b7467bf0874bc9","partName":"HOSE ASSEMBLY"},
{"_id":"59a942aac0b7467bf0874b58","partName":"WASHER"},
{"_id":"59a942aac0b7467bf0874b96","partName":"BEARING"},
{"_id":"59a942a7c0b7467bf086e06d","partName":"DIODE ASSY"},
{"_id":"59a94293c0b7467bf08351f0","partName":"BRUSH ASSY"},
{"_id":"59a942a7c0b7467bf086e9b1","partName":"BOARD"},
{"_id":"59a942a8c0b7467bf0870900","partName":"CABLE ASSY"},
{"_id":"59a942aac0b7467bf0874ad6","partName":"NUT"},
{"_id":"59a942aac0b7467bf0874af7","partName":"SCREW"},
{"_id":"59a942aac0b7467bf0874aa4","partName":"BUSHING"},
{"_id":"59a942aac0b7467bf0874ad7","partName":"STUD"},
{"_id":"59a942aac0b7467bf0874b7c","partName":"BOLT"},
{"_id":"59a942aac0b7467bf0874928","partName":"CYLINDER"},
{"_id":"59a942aac0b7467bf08749ba","partName":"RETAINING RING"},
{"_id":"59a942aac0b7467bf0874a39","partName":"CLAMP"},
{"_id":"59a942aac0b7467bf08749dd","partName":"PLUG"},
{"_id":"59a94293c0b7467bf0835208","partName":"SPR BOOSTER"},
{"_id":"59a942a9c0b7467bf087325b","partName":"FLANGE SCREW"},
{"_id":"59a942aac0b7467bf0874a75","partName":"O-RING"},
{"_id":"59a942aac0b7467bf0874b51","partName":"CAP"},
{"_id":"59a942aac0b7467bf0874945","partName":"BAFFLE PLATE"},
{"_id":"59a9429ac0b7467bf084f061","partName":"CLAMP, HOSE"},
{"_id":"59a942a9c0b7467bf0873a70","partName":"BOOT"},
{"_id":"59a942aac0b7467bf0874b73","partName":"SWITCH"},
{"_id":"59a942aac0b7467bf0874156","partName":"HOSE ASSY"},
{"_id":"59a942aac0b7467bf0874b3b","partName":"PISTON"},
{"_id":"59a942aac0b7467bf08740c0","partName":"ROTOR"}
]
保存在mongodb中的文档:
/* 1 */
{
"_id" : ObjectId("59b5f107366901668ab00074"),
"clusterName" : "9129",
"__v" : 0,
"partCluster" : [
"9132",
"9190",
"9279"
]
}
/* 2 */
{
"_id" : ObjectId("59b5f107366901668ab00075"),
"clusterName" : "9130",
"__v" : 0,
"partCluster" : [
"9180",
"9430"
]
}
/* 3 */
{
"_id" : ObjectId("59b5f107366901668ab00078"),
"clusterName" : "9176",
"__v" : 0,
"partCluster" : [
"9187"
]
}
/* 4 */
{
"_id" : ObjectId("59b5f107366901668ab00079"),
"clusterName" : "9227",
"__v" : 0,
"partCluster" : [
"9237",
"9257"
]
}
/* 5 */
{
"_id" : ObjectId("59b5f107366901668ab0007d"),
"clusterName" : "9424",
"__v" : 0,
"partCluster" : [
"9542"
]
}
/* 6 */
{
"_id" : ObjectId("59b5f107366901668ab0007e"),
"clusterName" : "SPRING",
"__v" : 0,
"partCluster" : [
"RING"
]
}
/* 7 */
{
"_id" : ObjectId("59b5f107366901668ab0007f"),
"clusterName" : "HOUSING",
"__v" : 0,
"partCluster" : [
"BUSHING"
]
}
/* 8 */
{
"_id" : ObjectId("59b5f107366901668ab00083"),
"clusterName" : "PISTON SYS",
"__v" : 0,
"partCluster" : [
"PISTON"
]
}
/* 9 */
{
"_id" : ObjectId("59b5f107366901668ab00084"),
"clusterName" : "CAPSCREW",
"__v" : 0,
"partCluster" : [
"SCREW"
]
}
/* 10 */
{
"_id" : ObjectId("59b5f107366901668ab00086"),
"clusterName" : "CLAMP",
"__v" : 0,
"partCluster" : [
"CAP"
]
}
/* 11 */
{
"_id" : ObjectId("59b5f107366901668ab00087"),
"clusterName" : "LOCKWASHER",
"__v" : 0,
"partCluster" : [
"WASHER"
]
}
/* 12 */
{
"_id" : ObjectId("59b5f107366901668ab0008a"),
"clusterName" : "BOLT",
"__v" : 0,
"partCluster" : [
"BOOT"
]
}
/* 13 */
{
"_id" : ObjectId("59b5f107366901668ab0008b"),
"clusterName" : "HOSE ASSEMBLY",
"__v" : 0,
"partCluster" : [
"HOSE ASSY"
]
}
/* 14 */
{
"_id" : ObjectId("59b5f107366901668ab0008d"),
"clusterName" : "9235",
"__v" : 0,
"partCluster" : [
"9425",
"9435"
]
}