【发布时间】: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。我得看看司机。以前从未使用过它们。谢谢