我制作了一些像这样的文件:
var mongoose = require('mongoose');
var ChildrentSchema = new mongoose.Schema
({
name: String
});
module.exports = mongoose.model("Childrent", ChildrentSchema);`
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();`
所以我需要得到保罗的所有孙子,但我不知道如何在&lt; ...&gt; 中编写一些代码。
有人帮助我!请!
答案 0 :(得分:0)
这应该有效:
var gran1 = new Grand
({
name: "Paul",
childs: dad1._id,
grandchilds: dad1.child
})