首页 > 解决方案 > MongoDB Compass:插入文档时如何为嵌套对象生成“_id”

问题描述

我正在尝试使用 MongoDB Compass 将一些数据插入到我的 MongoDB API 中。我的问题是只有外部对象会自动生成“_id”。所以帖子等中的嵌套对象没有“_id”。是否可以为所有对象生成 id?

这是我插入的数据的演示版本:

{
    "company_name":"Company name",
    "columns":[
        {
            "name":"Opportunities",
            "percentage":{
                "$numberDecimal":"0"
            },
            "total":{
                "$numberDecimal":"0"
            }
        },
        {
            "name":"Prospects",
            "percentage":{
                "$numberDecimal":"0.25"
            },
            "total":{
                "$numberDecimal":"0"
            }
        },
        {
            "name":"Proposals",
            "percentage":{
                "$numberDecimal":"0.5"
            },
            "total":{
                "$numberDecimal":"0"
            }
        },
        {
            "name":"Presentations",
            "percentage":{
                "$numberDecimal":"0.75"
            },
            "total":{
                "$numberDecimal":"0"
            }
        },
        {
        "name":"Won",
            "percentage":{
                "$numberDecimal":"1"
            },
            "total":{
                "$numberDecimal":"0"
            }
        },
        {
        "name":"Lost",
            "percentage":{
                "$numberDecimal":"1"
            },
            "total":{
                "$numberDecimal":"0"
            }
        },
        {
            "name":"No Opportunity",
            "percentage":{
                "$numberDecimal":"1"
            },
            "total":{
                "$numberDecimal":"0"
            }
        }
    ],
    "board_posts":[
        {
          "company_type": "Surveying",
          "lines_of_business": "Strategic Advisors",
          "author_id":
          {
            "$oid": "5ea02eaa1c9d440000911537"
          },
          "division": "Advisory"
        },
        {
          "company_type": "Damage Management",
          "lines_of_business": "Strategic Sales",
          "author_id":
          {
            "$oid": "5ea02eaa1c9d440000911537"
          },
          "division": "Advisory"
        },
        {
          "company_type": "Claims Management",
          "lines_of_business": "Strategic Sales",
          "author_id":
          {
            "$oid": "5ea02eaa1c9d440000911537"
          },
          "division": "Advisory"
        }
    ],
    "divisions":[
        {
            "name":"Advisory"
        },
        {
            "name":"M&A"
        },
        {
            "name":"Partnerships"
        }
    ]
}

外部对象将生成一个“_id”,例如:

{
    "_id":{
        "$oid": "5ea02eaa1c9d440000919999"
    },
    "company_name":"Company name",
    "columns":[ objects ],
    "board_posts":[ objects ],
    "divisions":[ objects ]
}

但我希望为列、board_posts、部门中的每个对象生成一个 _id。

标签: mongodbmongodb-compass

解决方案


MongoDB 指南针无法做到这一点......我浪费了很长时间来寻找解决方案并最终编写了一个脚本来做同样的事情。

这是脚本要点

https://gist.github.com/bansalvks/c6b5b2bdddc18e0957e26434d165ca1d

希望这将有助于来访者:)


推荐阅读