英文:
how to populate in mongoose?
问题
var headSchema = new Schema({
typename: String,
headname: String,
status: { type: Number, default: 1 }
});
var Head = mongoose.model("heads", headSchema);
var mgmtSchema = new Schema({
headtype: String,
type: { type: Schema.Types.ObjectId, ref: "Head" },
datefrom: Date,
dateto: Date,
title: String,
amount: Number,
description: String,
created_at: { type: Date, default: Date.now },
updated_at: { type: Date, default: Date.now },
status: Number
});
var HeadMgmt = mongoose.model("headmgmt", mgmtSchema);
HeadMgmt.find({})
.populate({ path: "type", select: "headname" })
.exec(function(err, result) {
console.log(result);
});
英文:
var headSchema = new Schema({
typename: String,
headname: String,
status: { type: Number, default: 1 }
});
var Head = mongoose.model("heads", headSchema);
var mgmtSchema = new Schema({
headtype: String,
type: { type: Schema.Types.ObjectId, ref: "Head" },
datefrom: Date,
dateto: Date,
title: String,
amount: Number,
description: String,
created_at: { type: Date, default: Date.now },
updated_at: { type: Date, default: Date.now },
status: Number
});
var HeadMgmt = mongoose.model("headmgmt", mgmtSchema);
HeadMgmt.find({})
.populate({ path: "type", select: "headname" })
.exec(function(err, result) {
console.log(result);
});
I have two schemas, also foreign key reference. The second schema has some value. I need to display the lists in one html table. How can print headname from head schema?
答案1
得分: 0
在mgmtSchema
中,您需要将类型字段中的ref值从Head
更改为heads
,因为您在设置Head模型时使用了heads
,如下所示:
var Head = mongoose.model("heads", headSchema);
所以您的mgmtSchema
必须像这样:
var mgmtSchema = new Schema({
headtype: String,
type: { type: Schema.Types.ObjectId, ref: "heads" },
datefrom: Date,
dateto: Date,
title: String,
amount: Number,
description: String,
created_at: { type: Date, default: Date.now },
updated_at: { type: Date, default: Date.now },
status: Number
});
您当前的populate代码的结果将类似于:
[
{
"_id": "5e13483dd62cd92e3c3e8dcd",
"headtype": "headtype",
"type": {
"_id": "5e1347671b0bd339e0521139",
"headname": "headname 1"
},
"title": "title",
"amount": 123,
"description": "description",
"created_at": "2020-01-06T14:46:21.906Z",
"updated_at": "2020-01-06T14:46:21.906Z",
"__v": 0
}
]
英文:
In mgmtSchema
you need to change the ref value in type field from Head
to heads
, because you used heads
setup your Head model like this:
var Head = mongoose.model("heads", headSchema);
So your mgmtSchema must be like this:
var mgmtSchema = new Schema({
headtype: String,
type: { type: Schema.Types.ObjectId, ref: "heads" },
datefrom: Date,
dateto: Date,
title: String,
amount: Number,
description: String,
created_at: { type: Date, default: Date.now },
updated_at: { type: Date, default: Date.now },
status: Number
});
The result of your current populate code will be something like this:
[
{
"_id": "5e13483dd62cd92e3c3e8dcd",
"headtype": "headtype",
"type": {
"_id": "5e1347671b0bd339e0521139",
"headname": "headname 1"
},
"title": "title",
"amount": 123,
"description": "description",
"created_at": "2020-01-06T14:46:21.906Z",
"updated_at": "2020-01-06T14:46:21.906Z",
"__v": 0
}
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论