在猫鼬中引用属性
Referencing properties in mongoose
我做了一些 file.js 这样的:
- 文件Children.js
var mongoose = require('mongoose');
var ChildrentSchema = new mongoose.Schema
({
name: String
});
module.exports = mongoose.model("Childrent", ChildrentSchema);`
- 文件Dad.js
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);`
- 文件Grand.js
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);`
- 并在文件中 app1.js
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();`
所以我需要得到 Paul 的所有孙子,但我不知道如何在 <...>.
中编写一些代码
有人帮帮我!请!
这应该有效:
var gran1 = new Grand
({
name: "Paul",
childs: dad1._id,
grandchilds: dad1.child
})
我做了一些 file.js 这样的:
- 文件Children.js
var mongoose = require('mongoose');
var ChildrentSchema = new mongoose.Schema
({
name: String
});
module.exports = mongoose.model("Childrent", ChildrentSchema);`
- 文件Dad.js
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);`
- 文件Grand.js
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);`
- 并在文件中 app1.js
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();`
所以我需要得到 Paul 的所有孙子,但我不知道如何在 <...>.
中编写一些代码有人帮帮我!请!
这应该有效:
var gran1 = new Grand
({
name: "Paul",
childs: dad1._id,
grandchilds: dad1.child
})