MySQL到MongoDB将整数ID转换为ObjectID并重建引用

时间:2019-05-24 20:57:59

标签: mysql node.js mongodb primary-key objectid

我正在现有数据库上构建新的NodeJS应用程序。我已经将现有数据库从MySQL转换为MongoDB。我使用MySQL Workbench以JSON格式导出sql数据,然后使用mongorestore将数据还原到MongoDB。这行得通。

现有的MySQL数据库使用autoIncrement属性为主键生成一个整数ID号。

例如,“人物”表具有主键“ PeopleID”,它是一个从0到大约三位数的整数,例如1、12或123。

许多其他表也使用相同的技术。 “位置”表具有自动递增的相同格式的“位置ID”。

在相关表中,主键作为标准关系数据库存储为外键。

这是MongoDB中新导入的文档。 Mongo为每个文档生成一个_id。

{
    "_id": "5ce89632c15df953bbe163e1", // newly created MongoDB ObjectID
    "PersonID": 1, // old primary key
    "LocationID": 12, // old foreign key
    "FirstName": "John",
    "MiddleName": "",
    "LastName": "Smith"
}

我想使用ObjectID而不是自动递增的整数重建所有引用。因此理想情况下,新文档应类似于“外键”作为ObjectID引用的样子。

{
    "_id": "5ce89632c15df953bbe163e1", // Use this as the reference instead of the old "PersonID"
    "PersonID": 1, // old primary key
    "LocationID": "5ce8358ec15df953bab163ea", // example ObjectID as a reference
    "FirstName": "John",
    "MiddleName": "",
    "LastName": "Smith"
}

是否可以使用带有ObjectID的引用而不是旧的整数值以编程方式重建关系?

1 个答案:

答案 0 :(得分:0)

我能够通过在Mongoose中编写一组查询来基于整数外键构建引用。第一步是构建字典数组,以将每个旧外键映射到其ObjectID。下一步是遍历目标集合中的每个文档,对于每个项目,在先前创建的idmap中查找ObjectID。最后一步是使用新的ObjectID更新目标文档。

在这种情况下,我创建了LocationID到ObjectID的映射,并使用新的Location_id更新了People集合。我将其设置为路由器中的GET请求。这是一种快速的解决方案,一次只能处理一个映射。如果有机会,我可以使其更具可伸缩性和参数化,甚至可以将其包装到模块中。让我知道是否有人认为这是一个有用的开始。从MySQL到MongoDB的迁移,我将继续根据需要使用它。

const express = require('express');
const router = express.Router();

/**
 * *** INSTRUCTIONS ***
 *
 * Convert a MySQL relationship to a NoSQL reference
 *
 * People table is the target table that contains the relational primary key PersonID
 * LocationID is a relational foreign key stored as an integer
 * Iterate all documents in People collection
 * Match LocationID integer in the Locations collection to get its unique ObjectID
 * Store the retrieved ObjectID as Location_id in People collection

    MySQL Relationship to convert

    PersonID: {
      type: DataTypes.INTEGER(10).UNSIGNED,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
      unique: true
    },
    LocationID: {
      type: DataTypes.INTEGER(10).UNSIGNED,
      allowNull: true,
      references: {
        model: 'locations',
        key: 'LocationID'
      }

    MongoDB Reference to create

    // Relational primary key autoIncrement ID
    PersonID: {
      type: Number,
      index: true,
      unique: true
    },
    // Relational foreign key autoIncrement ID
    LocationID: {
      type: Number
    },
    // ObjectID reference
    Location_id: {
      type: Schema.Types.ObjectId,
      ref: 'Locations'
    }

 * People
 * Primary Key table = people
 * Foreign Key table = locations
 * Foreign Key field to convert = LocationID
 * New Foreign Key field  = Location_id
 *
 */

// Perform update if true if false read records and log only
const pktable = require('../../models/people');
const fktable = require('../../models/locations');
const prvfkfield = 'LocationID';
const newfkfield = 'Location_id';

router.get('/_id', (req, res, next) => {
  const origin = 'routes/migrate_id.js GET /';

  // Build a dictionary to map old Foreign Key integer to its ObjectID
  async function migrate() {

    // Initialize array to store map of Foreign Keys to ObjectIDs
    let idmap = [];
    // Initialize integer to store number of targets
    let targetcount = 0;
    // Initialize integer to store number of successful results
    let successcount = 0;
    // Initialize integer to store number of skipped results
    let skippedcount = 0;

    // Create a cursor on Foreign Key table
    const fkcursor = fktable.find({}, {[prvfkfield]: 1}).cursor();

    // Build a map of Foreign Keys to ObjectIDs
    await fkcursor.eachAsync(async function (id) {
      idmap.push(id.toObject());
    });

    // Create a cursor on Primary Key table
    const pkcursor = pktable.find().cursor();

    // Iterate each item in cursor and return target record to update
    await pkcursor.eachAsync(async function (target) {

      // Get Previous Foreign Key
      const prvfk = target[prvfkfield];

      targetcount = targetcount + 1;

      // Get ObjectID mapped to the Previous Foriegn Key field
      let objectid = idmap.find(item => item[prvfkfield] === prvfk);

      if (objectid) {
        // Set ObjectID on target document
        target[newfkfield] = objectid;

        try {
          await target.save();
          successcount = successcount + 1;
        } catch (saveerror) {
          console.error(`ERROR: ${JSON.stringify(saveerror)}`);
        }

      } else {
        skippedcount = skippedcount + 1;
      }

    });

    const result = {
      'idmapcount': idmap.length,
      'targetcount': targetcount,
      'successcount': successcount,
      'skippedcount': skippedcount
    };

    return result;

  }

  migrate().then((result) => {
    res.status(200).send(result);
  }).catch((error) => {
    console.error(`migrate failed ${error}`);
    res.status(500).send(`migrate failed ${error}`);
  });

});

module.exports = router;