gpt4 book ai didi

node.js - 在链式 Promisified 函数内创建 MongoDB 文档

转载 作者:太空宇宙 更新时间:2023-11-04 00:14:49 25 4
gpt4 key购买 nike

应用每 15 分钟运行一个进程,创建许多具有以下格式的输出文件:

<timestamp>-<piece-code>.txt

该进程创建的所有文件都将共享“时间戳”组件,但“片段代码”会有所不同,这意味着我有一个时间戳与许多时间戳的关系我想将其保存在 mongodb 中:

"Registry" { time: (numeric) }
"Piece" { file: (string), piece: (string), registry: (reference) }

在此过程中,我想在创建时间戳之前查找它是否已存在以避免重复,但我的代码似乎无法做到这一点。

预先感谢您的帮助!

const mongoose = require('mongoose');

const Registry = require('../model/Registry');
const Piece = require('../model/Piece');
mongoose.Promise = require('bluebird');

// Set up default mongoose connection
let mongoDB = 'mongodb://127.0.0.1/numbers';
mongoose.connect(mongoDB, {
useMongoClient: true
});

//Get the default connection
let db = mongoose.connection;

// Bind connection to error event
db.on('error', console.error.bind(console, 'MongoDB connection error:'));


// Error handler
let error = (err) => {
console.log("** Error: ", err);
db.close();
};

// Finish routine
let finish = () => {
console.log("I have finished");
db.close();
};


// Create a number if it does not exist already
let newRegistry = (timestamp) => {
return new Promise((resolve, reject) => {
Registry.count({}).then((c) => { console.log(c) })

// Does this Registry exist?
Registry.findOne({ time: timestamp })
.then((reg, err) => {
if (err) {
reject(err);
}
// Create when it does not exist
if (!reg) {
Registry.create({
time: timestamp
}).then((doc, err) => {
if (err) {
reject(err);
}
resolve(doc._id);
})
.catch(error);
} else {
// Else, resolve
resolve(reg._id);
}

});
});
}

let newPiece = (registryId, filename, piece) => {
return Piece.create({
registry: mongoose.Types.ObjectId(registryId),
file: filename,
piece: piece
});

};

let createRegs = (files) => {
return new Promise((resolve, reject) => {
let promises = [];
let total = files.length;
for (let i=0; i<total; i++) {
let fileAr = files[i].split("-");
let time = fileAr[0];
let piece = fileAr[1];

let promise = newRegistry(time)
.then((regId) => {
return newPiece(regId, files[i], piece);
});

promises.push(promise);
}

Promise.all(promises)
.then(() => {
resolve();
})
.catch(error);
});
}


// Register input time and file pieces: one "time" can have many "pieces"
let filenames = ["1512473256-e.txt", "1512471758-a.txt", "1512471892-a.txt", "1512471758-b.txt"];

// Must end up with 3 "Registry" and 4 "Piece" documents
createRegs(filenames)
.then(finish)
.catch(error);

最佳答案

也许值得摆脱数组中的重复数字?通过这样做:

let filtered = [];
numbers.forEach((num) => {
// in case we don't have such entity creating it
if (!filtered.includes(num)) {
filtered.push(num);
}
// in case we have just returning resolved promise
return Promise.resolve();
});

然后在数据库中创建您的实体:

const promises = filtered.map(n => {
Number.findOne({ number: n })
.then(result => if(!result) {
return Number.create({ number: n });
}).catch(err => console.log(err));
});
Promise.all(promise).then(() => /* Doing what you want to do after saving */)

关于node.js - 在链式 Promisified 函数内创建 MongoDB 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47649326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com