Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 将带有内部关系数据的JSON推送到MongoDB?_Node.js_Json_Mongodb_Database Design_Mongoose Schema - Fatal编程技术网

Node.js 将带有内部关系数据的JSON推送到MongoDB?

Node.js 将带有内部关系数据的JSON推送到MongoDB?,node.js,json,mongodb,database-design,mongoose-schema,Node.js,Json,Mongodb,Database Design,Mongoose Schema,我有一个JSON,我想放到我的mongo db数据库中。我的JSON包含一些关系数据。这些数据是JSON的内部数据,我不需要将其存储在任何其他地方。例如: { title: "John film list" films [ { title: "Once upon a time in Hollywood" director: '1a' //referencing the director using an ID of type st

我有一个JSON,我想放到我的mongo db数据库中。我的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。但是,它为我的模式提供了新的id(
\u id
-field),我现在不知道如何在mongo中正确地关联这两个id?

MongoDB文档中的默认唯一主键是\u id。插入新文档时,它返回插入(创建)的记录的唯一id

此id中的值是基于时间创建的ObjectId。你可以读到它

如果要对_id使用自己的值,则必须在调用insert时传递它,如下所示:

db.directors.insertOne({_id: '1a', name: 'Tarantino'})