Return Mongoose 中的填充对象数组
Return Array of Populated Objects in Mongoose
我有一个论坛数据库,其中包含 3 个集合:主题、帖子、评论。
我有一个 return 一个 个人 论坛线程的 GET 请求,该线程用用户的 post 填充每个线程,每个用户 post 填充任何评论是在上面制作的,工作如下所示:
router.get('/:id', (req, res) => {
Threads
.findById(req.params.id)
.lean()
.populate({path: 'posts'})
.exec(function(err, docs){
var options = {
path: 'posts.comments',
model: 'comments'
};
if(err) return res.json(500);
Threads.populate(docs, options, function(err, thread){
res.json(thread);
})
})
})
发出此 GET 请求后,它将 return 一个论坛主题,如下所示:
{
"_id": "5924ad549a08ed4e70a9c89f",
"title": "Testing Full Schemas",
"author": "Mongoose",
"content": "Schema Content",
"posts": [
{
"_id": "5924ad999a08ed4e70a9c8a0",
"content": "New Schema Post",
"user": "Mongodb",
"comments": [
{
"_id": "5924ae489a08ed4e70a9c8a1",
"comment": "New Schema Content",
"user": "Matt",
"likes": 0,
"created": "2017-05-25T12:41:58.319Z"
}
]
}
现在我需要一个 GET 请求到 return ALL 个线程数组 (router.get('/')) 每个线程 posts 和要填充的评论。我试图替换:
Threads
.findById(req.params.id)
和
Threads
.find(req.params.id)
但它不起作用。有人知道如何实现吗?
至return所有线程,只需使用find
,不包含任何match condition
。
此外,find
中的 populate posts
和 'posts.comment'
查询本身,您不需要在 find
的 callback
中执行。
跨多个级别使用人口
**试试这个:
Threads.find({})
.populate({
path:'posts',
populate :{
path : comments
}
})
.exec(function(err,docs){
//docs is the array of all the Threads, with posts and comments populated within it
})
阅读 Mongoose Documentation on Populate and Nested Population 了解详细信息。 (搜索 跨多个级别填充)
findById 和 findOne returns 单个文档,其中 find returns一个游标。一旦你通过 find 的光标,你就到了最后,没有更多的文件了。
使用查找查询:-
ModelName.find({_id:req.params.id})
.populate({
path:'posts',
populate :{
path : comments
}
},(error,data)=>{
if(error){
res.json(error);
}
else{
res.json(data);
}
})
使用findById查询:-
ModelName.findById(req.params.id)
.populate({
path:'posts',
populate :{
path : comments
}
},(error,data)=>{
if(error){
res.json(error);
}
else{
res.json(data);
}
})
我有一个论坛数据库,其中包含 3 个集合:主题、帖子、评论。 我有一个 return 一个 个人 论坛线程的 GET 请求,该线程用用户的 post 填充每个线程,每个用户 post 填充任何评论是在上面制作的,工作如下所示:
router.get('/:id', (req, res) => {
Threads
.findById(req.params.id)
.lean()
.populate({path: 'posts'})
.exec(function(err, docs){
var options = {
path: 'posts.comments',
model: 'comments'
};
if(err) return res.json(500);
Threads.populate(docs, options, function(err, thread){
res.json(thread);
})
})
})
发出此 GET 请求后,它将 return 一个论坛主题,如下所示:
{
"_id": "5924ad549a08ed4e70a9c89f",
"title": "Testing Full Schemas",
"author": "Mongoose",
"content": "Schema Content",
"posts": [
{
"_id": "5924ad999a08ed4e70a9c8a0",
"content": "New Schema Post",
"user": "Mongodb",
"comments": [
{
"_id": "5924ae489a08ed4e70a9c8a1",
"comment": "New Schema Content",
"user": "Matt",
"likes": 0,
"created": "2017-05-25T12:41:58.319Z"
}
]
}
现在我需要一个 GET 请求到 return ALL 个线程数组 (router.get('/')) 每个线程 posts 和要填充的评论。我试图替换:
Threads
.findById(req.params.id)
和
Threads
.find(req.params.id)
但它不起作用。有人知道如何实现吗?
至return所有线程,只需使用find
,不包含任何match condition
。
此外,find
中的 populate posts
和 'posts.comment'
查询本身,您不需要在 find
的 callback
中执行。
跨多个级别使用人口
**试试这个:
Threads.find({})
.populate({
path:'posts',
populate :{
path : comments
}
})
.exec(function(err,docs){
//docs is the array of all the Threads, with posts and comments populated within it
})
阅读 Mongoose Documentation on Populate and Nested Population 了解详细信息。 (搜索 跨多个级别填充)
findById 和 findOne returns 单个文档,其中 find returns一个游标。一旦你通过 find 的光标,你就到了最后,没有更多的文件了。
使用查找查询:-
ModelName.find({_id:req.params.id})
.populate({
path:'posts',
populate :{
path : comments
}
},(error,data)=>{
if(error){
res.json(error);
}
else{
res.json(data);
}
})
使用findById查询:-
ModelName.findById(req.params.id)
.populate({
path:'posts',
populate :{
path : comments
}
},(error,data)=>{
if(error){
res.json(error);
}
else{
res.json(data);
}
})