【问题标题】:mongoose How to exclude _id from populated猫鼬如何从填充中排除_id
【发布时间】:2017-12-06 09:24:10
【问题描述】:

我正在我的角色文档中填充 _group

return this.find(query, {'_group': 1, 'name':1, 'description': 1} )
  .populate('_group', ['name', 'description'])
  .sort({ createdAt: -1 })
  ...

我还得到了 _group 的 _id

{"_id":"5959ef7db9938a0600f05eb2",
"_group":{
    "_id":"5959ef7db9938a0600f05eae",
    "name":"GroupA",
     "description":"Description GroupA"
 },
"name":"manager",
"description":"can R group, can RW user"},

我怎样才能摆脱 _group._id ?

我试过了:

 return this.find(query, {'_group': 1, 'name':1, '_group._id':0, '_id':0} )

但它会引发错误:投影不能混合包含和排除。

return this.find(query, {'_group': 1, 'name':1, '_id':0, '_id':0}

仅删除角色._id

感谢反馈

【问题讨论】:

标签: node.js mongoose mongoose-populate


【解决方案1】:
return this.find(query, {'_group': 1, 'name':1, 'description': 1} ).
    populate({
        path: '_group',
        select: 'name description -_id',
      }).sort({ createdAt: -1 })

在您的示例中使用此逻辑

【讨论】:

  • 非常感谢...但是对于文档 ID 使用减号而不是 0 是多么奇怪的方式...
猜你喜欢
  • 2022-01-23
  • 2019-09-16
  • 2015-07-13
  • 2016-10-10
  • 2016-08-17
  • 2018-06-01
  • 2021-01-29
  • 2016-06-11
相关资源
最近更新 更多