mongodb - 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。
解决方案
MongoDB 指南针无法做到这一点......我浪费了很长时间来寻找解决方案并最终编写了一个脚本来做同样的事情。
这是脚本要点
https://gist.github.com/bansalvks/c6b5b2bdddc18e0957e26434d165ca1d
希望这将有助于来访者:)
推荐阅读
- powershell - 将 Powershell 对象导出到 CSV - 拒绝访问
- sql - SQLCMD , 'tee' 不被识别为内部或外部命令
- mongodb - 如何在字符串数组中搜索字符串
- spring-mvc - 我的 spring-mvc 应用程序在 glassfish 4.1 服务器上抛出 'BeanCreationException: Error Creating bean...'requestMappingHandlerAdapter'..'
- javascript - 控制内容位于通过对象标记插入主站点 (index.html) 的外部 html 文件中
- reactjs - 如何传递参数以响应本机 redux 操作?
- python - 如何使用networkx反转有向图中的箭头?
- php - 为什么在提交表单时在 laravel 中找不到它的显示文件?
- c++ - 如何将预定义数据类型的值存储到数组中并输出
- android - 如何修复 android studio 上的清单合并失败错误?