Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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
Mongodb 由子文档ID填充的mongoose_Mongodb_Mongoose - Fatal编程技术网

Mongodb 由子文档ID填充的mongoose

Mongodb 由子文档ID填充的mongoose,mongodb,mongoose,Mongodb,Mongoose,假设以下模式: Aschema ASchema = { type: { type: ObjectId, ref: BModel } } /** * ADoc = { * _id: 00a1, * type: 00b1001 * }, * */ BSchema = { children: [{ name: String, }] } /** * BDoc = {

假设以下模式:

Aschema

  ASchema = {
    type: { type: ObjectId, ref: BModel }
  }

  /**
   * ADoc = { 
   *    _id: 00a1, 
   *    type: 00b1001 
   * },
   * 
   */ 
  BSchema = {
    children: [{ 
      name: String, 
    }]
  }

  /**
   * BDoc = { 
   *    _id: 00b1,
   *    children: [{ 
   *      _id: 00b1001,
   *      name: 'b1'
   *    }, {
   *      _id: 00b1002,
   *      name: 'b2'
   *    }]
   * }
   * 
   */
BSchema

  ASchema = {
    type: { type: ObjectId, ref: BModel }
  }

  /**
   * ADoc = { 
   *    _id: 00a1, 
   *    type: 00b1001 
   * },
   * 
   */ 
  BSchema = {
    children: [{ 
      name: String, 
    }]
  }

  /**
   * BDoc = { 
   *    _id: 00b1,
   *    children: [{ 
   *      _id: 00b1001,
   *      name: 'b1'
   *    }, {
   *      _id: 00b1002,
   *      name: 'b2'
   *    }]
   * }
   * 
   */
当我找到by_id:“00a1”时,我将得到以下结果

  AModel.find({_id: '00a1'}).lean();
  /**
   * {
   *    _id: 00a1,
   *    type: 00b1001
   * }
   */
如您所见,my ASchema type ref引用了B document中子文档的ObjectId

我想得到这个结果

  /**
   * {
   *    _id: 00a1,
   *    type: {
   *      _id: 00b1001,
   *      name: 'b1'
   *    }
   * }
   */
我知道有,但我得到空,希望得到大家的帮助,谢谢

我发布了同样的问题,但描述不够清楚。

mongoose应该如何理解您想要id为00b1001的子文档?谢谢@SuleymanSah,mongodb子文档具有唯一id。AModel的类型保存了B的子文档唯一id