【问题标题】:MongoDB Compass: How to generate "_id" for nested objects when inserting a documentMongoDB Compass:插入文档时如何为嵌套对象生成“_id”
【发布时间】:2020-04-22 12:38:22
【问题描述】:

我正在尝试使用 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。

【问题讨论】:

  • 您是否尝试通过 Compass JSON 导入来导入文档?如果是这样,我认为没有办法将生成的ObjectId() 指定为有效的 JSON。您是否考虑过使用易于指定的驱动程序,例如 python/pymongo。
  • 是的,我只是使用 Compass JSON 导入插入 JSON。我得看看司机。以前从未使用过它们。谢谢

标签: mongodb mongodb-compass


【解决方案1】:

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

这里是脚本要点

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

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

【讨论】:

    猜你喜欢
    • 2022-01-11
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    • 2012-06-15
    相关资源
    最近更新 更多