Javascript Mongoose查找所有匹配用户ID的文档,然后标记这些文档

Javascript Mongoose查找所有匹配用户ID的文档,然后标记这些文档,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,找到所有记录的最佳方法是什么,说Post,然后用一些标记只标记属于登录用户的帖子(假设一篇帖子可能属于多个用户)。理想情况下,我希望这种情况发生在服务器上,因此如果我总共有100篇带有Post.find({})的帖子,我可以得到如下所示的Mongoose文档或JSON对象: [{title: "some Post", userPost: true}, {title: "another post}, {title: "third post", userPost: true}, {title: "L

找到所有记录的最佳方法是什么,说
Post
,然后用一些标记只标记属于登录用户的帖子(假设一篇帖子可能属于多个用户)。理想情况下,我希望这种情况发生在服务器上,因此如果我总共有100篇带有
Post.find({})
的帖子,我可以得到如下所示的Mongoose文档或JSON对象:

[{title: "some Post", userPost: true}, {title: "another post}, {title: "third post", userPost: true}, {title: "Last post"}]
let currentUser;
Post.find().exec( ( error, posts ) => {
    posts.map( ( p ) => {
            let userPosted = currentUser.posts.some((userPost) => {
                return userPost.equals(p._id)
            });
            return Object.assign(p.toObject(), {userPost: userPosted});
    } );
} );
这样,我就可以用一种特定的方式装饰用户帖子。但是,我想查询所有帖子,但只查询用户帖子,因为这不是通过模板引擎,所以我需要在JSON响应中存在标志,以指示与当前用户的关系

目前,我有一个如下所示的函数:

[{title: "some Post", userPost: true}, {title: "another post}, {title: "third post", userPost: true}, {title: "Last post"}]
let currentUser;
Post.find().exec( ( error, posts ) => {
    posts.map( ( p ) => {
            let userPosted = currentUser.posts.some((userPost) => {
                return userPost.equals(p._id)
            });
            return Object.assign(p.toObject(), {userPost: userPosted});
    } );
} );

有改进的建议吗?该过滤功能是否应用于Post模型?在初始查询中是否有更有效的方法?我知道Mongoose中的
lean()
会返回JSON而不是Mongoose文档,所以我不需要进行转换。我对思想持开放态度。

我的假设大致如下:

    //first you need something associating the current user
    const currentUser = ?;
    const alteredPosts = [];
    //get all posts from Mongo
    Post.find({})
    .exec(error, posts => {
         if(error) console.error(error);
         //search through all posts and find those by the current user
         alteredPosts = posts.map( ( p ) => {
            if(p._id === currentUser._id) {
               p.currentUser = currentUser.id
            }
            return p;
         });
    })
 });

是的,谢谢你的回复。看起来您的代码只返回用户帖子。我想返回所有带有特别标记的用户帖子。在这种情况下,我想我会使用一些,看看帖子的ID是否与user.posts的某些/任何ID匹配。基本上我会返回posts.map
让markedPosts=posts.map(……
然后
res.json(markedPosts)
我认为没有映射的筛选器或某些筛选器是您要查找的。所有将userPost标记为true的帖子都将在返回的数组中。如果userPost键不存在,则返回p.userPost将与return undefined相同,因为键不存在,因此它将不在返回的数组中。我不遵循最后的注释。
user.posts
是一个数组,
posts
是一个数组,其中每个post都只有一个标题和ID——这里没有用户数据。对于每个post,我需要遍历user.posts以查看ID是否与post匹配。\u ID。作为回报,我需要一个包含所有post的数组,而不仅仅是用户post。我不知道筛选器如何为我提供所有post。它将呃,只给我用户帖子或未定义的帖子,但我仍然需要非用户帖子数据。我遗漏了什么吗?很难理解你想做什么。调用模型帖子有点混乱。因此,这是一个router.post而不是router.get,你在做什么?那么,将有一个新帖子,你想找到所有具有相同内容的帖子id作为post请求中发送的用户?哦,对不起,模型
post
就像一篇文章post-与HTTP方法类型无关。因此,我们可以忘记数据是如何与服务器通信的。我只是在寻找一个最佳约定来修改从数据库返回的数据,将一些数据标记为具有associ和
currentUser
类似于
Post.aggregate({$project:{“title”:1,userPost:{$eq:['$\u id',currentUser.id]}})的东西进行初始化。exec((错误,posts)=>{})
谢谢@Veeram。看起来这是在请求聚合返回标题的帖子,并将
userPost
设置为帖子的ID是否等于用户的ID。对吗?np.是的,这是正确的。在我的例子中,帖子架构有一个
\u ID
,并且
title
。用户架构在
curre中有一个post objectid数组ntUser.courses
。因此,我需要在聚合中查看帖子的ID是否与任何用户的帖子ID匹配。因此,我认为查看帖子的ID是否与用户的ID匹配不会有帮助,因为它们永远不会相同。我明白了。请尝试
post.aggregate({$project:{“title”:1,userPost:{$in:['$\u ID',currentUser.posts]}})。exec((错误,posts)=>{})
在3.4 mongo服务器版本中。