我正在学习MongoDB中的协会。在那里我学会了如何使用Embed,但在参考References时,我被困了几天,因为它和Embed一样工作。整个对象被添加到数组中,而不是仅显示数组中的ObjectId。 这是片段,
露营地架构:
var campgroundSchema = new mongoose.Schema({
name: String,
image: String,
description: String,
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}
]
});
评论架构:
var commentSchema = mongoose.Schema({
text: String,
author: String
});
模型
var Campground = mongoose.model("Campground", campgroundSchema);
var Comment = mongoose.model("Comment", commentSchema);
输出:
{ "_id" : ObjectId("5a71970ec0379521607b5f47"), "comments" : ***[ { "_id" :
ObjectId("5a71970ec0379521607b5f4a"), "text" : "this place is gr8", "author"
: "Homer", "__v" : 0 } ]***, "name" : "Clould's Rest", "image" :
"https://farm7.staticflickr.com/6014/6015893151_044a2af184.jpg",
"description" : "blah blah", "__v" : 1 }
我的意思是说,不是在comments数组中显示整个对象,数组必须只显示引用,即ObjectId 像这样
{ "_id" : ObjectId("5a71970ec0379521607b5f47"), "comments"
: ObjectId("5a71970ec0379521607b5f4a") ], "name" : "Clould's Rest", "image" :
"https://farm7.staticflickr.com/6014/6015893151_044a2af184.jpg",
"description" : "blah blah", "__v" : 1 }
这是参考工作的正确方法吗?