我想把一个JSON放到我的mongo数据库中。我的JSON保存了一些关系数据。这些数据是JSON的内部数据,我不需要将它存储在其他地方。示例:
{
title: "John film list"
films [
{
title: "Once upon a time in Hollywood"
director: '1a' //referencing the director using an ID of type string
},
{
title: "Some film with empty director field",
director: ''
}
],
directors: [
{
id: '1a', //find the director here
name: 'Tarantino'
}
]
}我不需要集中存储任何东西(我不需要在某个地方列出一大串董事),但在这个文档中,我需要能够查找导演(1a),并返回Tarantino。
我设法将这个JSON格式推到了MongoDB。但是,它为我的模式提供了新的it (_id-field),我现在很困惑如何在mongo中正确地将这两者联系起来?
发布于 2020-10-30 23:20:35
MongoDB文档中的默认唯一主键是_id。插入新文档时,它将返回插入(创建)的记录的唯一id。
这个id中的值是基于时间创建的ObjectId。你可以读到它,这里。
如果要为_id使用自己的值,则必须在调用插入时传递它,如下所示:
db.directors.insertOne({_id: '1a', name: 'Tarantino'})https://stackoverflow.com/questions/64611546
复制相似问题