Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.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 mongoose中的引用属性_Node.js_Mongodb_Properties_Reference_Family Tree - Fatal编程技术网

Node.js mongoose中的引用属性

Node.js mongoose中的引用属性,node.js,mongodb,properties,reference,family-tree,Node.js,Mongodb,Properties,Reference,Family Tree,我制作了一些file.js,如下所示: 文件Children.js 文件Dad.js 文件Grand.js var mongoose=require('mongoose'); var grandchema=新的mongoose.Schema ({ 名称:String, 儿童: { 类型:mongoose.Schema.Types.ObjectId, 裁判:“爸爸” }, }); module.exports=mongoose.model(“Grand”,grandchema)`

我制作了一些file.js,如下所示:

  • 文件Children.js

  • 文件Dad.js

  • 文件Grand.js

var mongoose=require('mongoose');
var grandchema=新的mongoose.Schema
({
名称:String,
儿童:
{
类型:mongoose.Schema.Types.ObjectId,
裁判:“爸爸”
},
});
module.exports=mongoose.model(“Grand”,grandchema)`
  • 在app1.js文件中

require(“./数据库”);
var Grand=需要(“./Grand”),
Dad=需要('./Dad'),
Childrent=需要('./Childrent');
var child1=新的Childrent
({
姓名:“亚历克斯”
});
child1.save();
var dad1=新爸爸
({
姓名:“罗宾”,
儿童:儿童1.\u id
});
dad1.save();
var gran1=新格兰德
({
姓名:“保罗”,
孩子们:爸爸,
})
grand1.save()`
所以我需要得到Paul的所有孙子,但我不知道如何在中编写一些代码

谁来帮帮我!求你了

这应该有效:

var gran1 = new Grand
   ({
       name: "Paul",
       childs: dad1._id,
       grandchilds: dad1.child
})
var mongoose = require('mongoose');
var DadSchema = new mongoose.Schema
({
    name: String,
    child:
    {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Childrent'
    }
});
module.exports = mongoose.model("Dad", DadSchema);`
var mongoose = require('mongoose');
var GrandSchema = new mongoose.Schema
({
    name: String,
    childs:
    {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Dad'
    },
    <...>
});
module.exports = mongoose.model("Grand", GrandSchema);`
require("./database");
var Grand = require('./Grand'),
Dad = require('./Dad'),
Childrent = require('./Childrent');
var child1 = new Childrent
({
    name: "Alex"
});
child1.save();
var dad1 = new Dad
({
    name: "Robin",
    child: child1._id
});
dad1.save();
var gran1 = new Grand
({
    name: "Paul",
    childs: dad1._id,
    <...>
})
grand1.save();`
var gran1 = new Grand
   ({
       name: "Paul",
       childs: dad1._id,
       grandchilds: dad1.child
})