简体   繁体   English

如何重构 MongoDB 中的集合

[英]How to restructure a collection in MongoDB

I'm looking to restructure my MongoDB collection and haven't been able to do so.我正在寻找重组我的 MongoDB 集合,但一直未能这样做。 I'm quite new to it and looking for some help.我对它很陌生,正在寻找一些帮助。 I'm struggling to access move the data within the "itemsList" field.我正在努力访问“itemsList”字段中的移动数据。

My collection documents are currently structured like this:我的收藏文档目前的结构如下:

{
  "_id": 1,
  "pageName": "List of Fruit",
  "itemsList":[
    {
      "myID": 101,
      "itemName": "Apple"
    },
    {
      "myID": 102,
      "itemName": "Orange"
    }
  ]
},
{
  "_id": 2,
  "pageName": "List of Computers",
  "itemsList":[
    {
      "myID": 201,
      "itemName": "MacBook"
    },
    {
      "myID": 202,
      "itemName": "Desktop"
    }
  ]
}

The end result最终结果

But I would like the data to be restructured so that the value for "itemName" is it's own document.但我希望重新构造数据,以便“itemName”的值是它自己的文档。

I would also like to change the name of "myID" to "itemID".我还想将“myID”的名称更改为“itemID”。

And save the new documents to another collection.并将新文档保存到另一个集合。

{
  "_id": 1,
  "itemName": "Apple",
  "itemID": 101,
  "pageName": "List of Fruit"
},
{
  "_id": 2,
  "itemName": "Orange",
  "itemID": 102,
  "pageName": "List of Fruit"
},
{
  "_id": 3,
  "itemName": "MacBook",
  "itemID": 201,
  "pageName": "List of Computers"
},
{
  "_id": 4,
  "itemName": "Desktop",
  "itemID": 202,
  "pageName": "List of Computers"
}

What I've tried我试过的

I have tried using MongoDB's aggregate functionality, but because there are multiple "itemName" fields in each document, it will add both of them to one Array - instead of one in each document.我曾尝试使用 MongoDB 的聚合功能,但因为每个文档中有多个“itemName”字段,它会将它们都添加到一个数组中 - 而不是每个文档中的一个。

db.collection.aggregate([
  {$Project:{
    itemName: "$itemsList.itemName",
    itemID: "$itemsList.otherID",
    pageName: "$pageName"
  }},
  {$out: "myNewCollection"}
])

I've also tried using PyMongo 3.x to loop through the document's fields and save as a new document, but haven't been successful.我也尝试过使用 PyMongo 3.x 来遍历文档的字段并另存为新文档,但没有成功。

Ways to implement it实现方式

I'm open to using MongoDB's aggregate functionality, if it can move these items to their own documents, or a Python script (3.x) - or any other means you think can help.我愿意使用 MongoDB 的聚合功能,如果它可以将这些项目移动到它们自己的文档或 Python 脚本 (3.x) - 或您认为可以提供帮助的任何其他方式。

Thanks in advance for your help!在此先感谢您的帮助!

You just need a $unwind to "break" the array.你只需要一个$unwind来“打破”数组。 Then you can do some data wrangling and output to your collection.然后您可以进行一些数据整理并输出到您的集合。

Note that as you didn't specify the exact requirement for the _id .请注意,由于您没有指定_id的确切要求。 You might need to take extra handling.您可能需要进行额外处理。 Below demonstration use the native _id generation, which will auto assigned ObjectIds.下面的演示使用原生_id生成,它将自动分配 ObjectId。

db.collection.aggregate([
  {
    "$unwind": "$itemsList"
  },
  {
    "$project": {
      "_id": 0,
      "itemName": "$itemsList.itemName",
      "itemID": "$itemsList.myID",
      "pageName": "$pageName"
    }
  },
  {
    $out: "myNewCollection"
  }
])

Here is the Mongo playground for your reference.这是Mongo 游乐场供您参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM