Express 猫鼬
参考中给出的MongooseURL填充示例,两个模式之间似乎存在双向关系。例如,如果我只有单向关系(使用相同的模式示例,但Person模式没有故事参考),该怎么办 如何返回如下所示的GET Story输出:Express 猫鼬,express,mongoose-populate,mongoose-models,Express,Mongoose Populate,Mongoose Models,参考中给出的MongooseURL填充示例,两个模式之间似乎存在双向关系。例如,如果我只有单向关系(使用相同的模式示例,但Person模式没有故事参考),该怎么办 如何返回如下所示的GET Story输出: { author :{ name: "Bla bla bla", age: 30 } title : "ABC Story" } 我现在总是这样: { author :34235245453 title : "ABC Story" } 我认为您混淆了p
{
author :{
name: "Bla bla bla",
age: 30
}
title : "ABC Story"
}
我现在总是这样:
{
author :34235245453
title : "ABC Story"
}
我认为您混淆了populated()函数和populate()函数 为了能够检索故事的作者信息,我们需要使用如下填充:
router.get(“/stories/:id”),异步(req,res)=>{
const result=await Story.findById(req.params.id).populate(“作者”);
res.send(结果);
});
假设我们有这样一个人:
{
"_id": "5e3c63ba3a178830dc497a00",
"name": "Name1",
"age": 33,
"__v": 0
}
以下是用户的故事:
{
"_id": "5e3c63e93a178830dc497a02",
"author": "5e3c63ba3a178830dc497a00",
"title": "Story1",
"__v": 0
}
当我们向路由发送get请求时,结果如下(http://.../stories/5e3c63f33a178830dc497a02
)
要获取包含作者信息的所有故事,我们可以使用如下的find()方法:
router.get(“/stories”),异步(req,res)=>{
const result=wait Story.find().populate(“作者”);
res.send(结果);
});
我认为您混淆了populated()函数和populate()函数
为了能够检索故事的作者信息,我们需要使用如下填充:
router.get(“/stories/:id”),异步(req,res)=>{
const result=await Story.findById(req.params.id).populate(“作者”);
res.send(结果);
});
假设我们有这样一个人:
{
"_id": "5e3c63ba3a178830dc497a00",
"name": "Name1",
"age": 33,
"__v": 0
}
以下是用户的故事:
{
"_id": "5e3c63e93a178830dc497a02",
"author": "5e3c63ba3a178830dc497a00",
"title": "Story1",
"__v": 0
}
当我们向路由发送get请求时,结果如下(http://.../stories/5e3c63f33a178830dc497a02
)
要获取包含作者信息的所有故事,我们可以使用如下的find()方法:
router.get(“/stories”),异步(req,res)=>{
const result=wait Story.find().populate(“作者”);
res.send(结果);
});
我不确定这是否是一个好的做法,但您可以通过以下方式实现
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const personSchema = Schema({
name: String,
age: Number
});
const storySchema = Schema({
author: Schema.Types.ObjectId, //modified by just defining the type
title: String
});
const Story = mongoose.model('Story', storySchema);
const Person = mongoose.model('Person', personSchema);
在填充时
SOMETHING.find()
.populate({
'path':'author',
'model':'Person'
});
我不确定这是否是一个好的做法,但您可以通过以下方式实现
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const personSchema = Schema({
name: String,
age: Number
});
const storySchema = Schema({
author: Schema.Types.ObjectId, //modified by just defining the type
title: String
});
const Story = mongoose.model('Story', storySchema);
const Person = mongoose.model('Person', personSchema);
在填充时
SOMETHING.find()
.populate({
'path':'author',
'model':'Person'
});
嗨@SuleymanSah,如果我想获得所有故事,我可以使用.find().populate('author')?我现在正在这样做,但当我发送“获取全部”请求时,我没有获取作者信息。我得到的输出是1iposted@BC2是的,您可以通过注释中的代码获取所有故事和作者信息。你能给我看一下你使用的全部代码吗?我将尝试编辑问题以包含我的示例。我已经通过使用Richard Job的示例获得了我想要的输出。谢谢你帮助我:D@BC2我的答案应该有效,他的答案与我的答案相似。嗨@SuleymanSah,如果我想获得所有故事,我可以使用.find().populate('author')?我现在正在这样做,但当我发送“获取全部”请求时,我没有获取作者信息。我得到的输出是1iposted@BC2是的,您可以通过注释中的代码获取所有故事和作者信息。你能给我看一下你使用的全部代码吗?我将尝试编辑问题以包含我的示例。我已经通过使用Richard Job的示例获得了我想要的输出。谢谢你帮助我:D@BC2我的答案应该有效,他的答案与我的答案相似。谢谢Richard。它起作用了。虽然我没有包括“for”模型<代码>等待故事.find().populate({'path':'author','model':Person})代码>如果我使用'作为值,则得到“模式尚未为模型注册”。现在一切都好了,谢谢Richard。它起作用了。虽然我没有包括“for”模型<代码>等待故事.find().populate({'path':'author','model':Person})代码>如果我使用'作为值,则得到“模式尚未为模型注册”。现在一切都好了。