提问者:小点点

如何才能找到和过滤属于用户的博客?


这些是数据库中的blog对象:

[
  {
    _id: 5fec92292bbb2c32acc0093c,
    title: 'Boxing ring',
    author: 'T. Wally',
    content: 'boxing stuff',
    likes: 0,
    user: {
      _id: 5fd90181d1e88a13109433f9,
      username: 'Johnny_23',
      name: 'John Q'
    },
    __v: 0
  },
  {
    _id: 5fec9481ce9c4a47a0ca6a2a,
    title: 'Football OP',
    author: 'RQ',
    content: 'it`s football',
    likes: 2,
    user: {
      _id: 5fec942dce9c4a47a0ca6a29,
      username: 'malcomm',
      name: 'Malcom'
    },
    __v: 0
  }
]

我只想找到并显示属于某个用户的博客

listRouter.get('/', async (request, response) => {
  const blogs = await Blog
    .find({}).populate('user', { username: 1, name: 1 })

    response.json(blogs)
})

我如何访问每个对象的用户id,并以一种我可以匹配博客的方式使用它?


共1个答案

匿名用户

由于属性具有,您可以这样构造查询:

会有一种情况,字段是一个引用(它的值是一个ObjectId。在本例中,请尝试

相关问题