在链式Promisified函数中创建MongoDB文档

时间:2017-12-05 08:42:41

标签: node.js mongodb mongoose promise bluebird

应用程序每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);

2 个答案:

答案 0 :(得分:1)

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

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();
});

然后在DB中创建您的实体:

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 */)

答案 1 :(得分:0)

我的方法是:

  1. 首先我要创建一个字典,其中键是时间戳,值是一个包含不同部分的数组。
  2. 一旦你有这本词典,我就会对待列表的每个时间戳
    • 查找数据库中的时间戳。如果它还不存在,请创建它。
    • 遍历时间戳的所有部分并将其添加到数据库中。
  3. 使用这种方法可以避免并发问题。 希望它有所帮助。