导入的MySQL数据库到MongoDB - 一对多关系id的更新

时间:2016-04-29 11:52:15

标签: mysql mongodb database

我使用MongoVUE将MySQL数据库导入MongoDB。

首先让我举一下我的表格:

Table0 = 1,500 entries
Table1 = 120,000 entries
Table2 = 18,000,000 entries

Table0 -> hasMany -> Table1 entries
Table1 -> hasMany -> Table2 entries

现在所有表都有一个_id密钥,但在导入后,这两个表仍然有来自MySQL的id密钥。

如何更新table2的密钥table1_id以匹配table1的_id密钥?是否可以使用Mongo查询或我是否必须为此编写脚本? (我所知道的唯一语言是PHP和Javascript / NodeJS)

更新1

使用user @ profesor79回答我在table1 = market_item_historiestable2 = market_items

的位置进行了此查询
db.market_item_histories.aggregate([    
    {
        $lookup: {
            from:"market_items",
            localField: "market_item_id",
            foreignField: "id",
            as: "market_items_docs"
        }
    },
    {
        $unwind:"$market_items_docs"
    },
    {
        $project: {
            _id:1,
            oldId:"$market_item_id",
            market_item_id:"$market_items_docs._id",
            date:1,
            price:1,
            amount:1,
            created_at:1,
            updated_at:1
        }        
    },
    {
        $out:"marketItemHistories"
    }
])

运行该代码时出现此错误:

assert: command failed: {
        "errmsg" : "exception: Unrecognized pipeline stage name: '$lookup'",
        "code" : 16436,
        "ok" : 0
} : aggregate failed
Error: command failed: {
        "errmsg" : "exception: Unrecognized pipeline stage name: '$lookup'",
        "code" : 16436,
        "ok" : 0
} : aggregate failed
    at Error (<anonymous>)
    at doassert (src/mongo/shell/assert.js:11:14)
    at Function.assert.commandWorked (src/mongo/shell/assert.js:254:5)
    at DBCollection.aggregate (src/mongo/shell/collection.js:1278:12)
    at (shell):1:26
2016-04-29T14:13:48.223+0000 E QUERY    Error: command failed: {
        "errmsg" : "exception: Unrecognized pipeline stage name: '$lookup'",
        "code" : 16436,
        "ok" : 0
} : aggregate failed
    at Error (<anonymous>)
    at doassert (src/mongo/shell/assert.js:11:14)
    at Function.assert.commandWorked (src/mongo/shell/assert.js:254:5)
    at DBCollection.aggregate (src/mongo/shell/collection.js:1278:12)
    at (shell):1:26 at src/mongo/shell/assert.js:13

1 个答案:

答案 0 :(得分:1)

这是一个很好的现实生活问题。 为了完成这项工作,我们可以使用聚合框架和“连接”表,然后在新集合中写入结果。 之后,可以重命名/删除源,也可以重命名我们的输出。 这是使用mongo控制台完成的。

请找到将table1与table0连接的解决方案,并使用此方法对其他连接执行。

db.table1.aggregate([    
    {
        $lookup:{
            from:"table0",
            localField: "table0_Id", // this is our join source
            foreignField: "id", // this id field in table0 collection
            as: "table0_docs"
            }
    },
    {
        $unwind:"$table0_docs"
        },
    {
        $project:{
            // very important list all fields here
            _id:1,
            data:1,
            oldId:"$table0_Id",
            referenceID:"$table0_docs._id",

            }        
        },
        {
            $out:"newCollectionName"
            }
    ])
  

AND OUTPUT DOCUMENT

{
    "_id" : ObjectId("57234f5de63d33670e521892"),
    "data" : "22",
    "oldId" : 1,
    "referenceID" : ObjectId("57234f33e63d33670e52188e")
}

欢迎任何评论!