如何查询MongoDB中返回结果子集的嵌入式文档



我有一个这样的数据结构:

Post
{
'id': $_id,
'user' : ['first_name' : 'Joe', 'last_name' : 'Devon' ],
'text' : 'blah',
'comment' : 
  ['first' : 'Joe', 'last' : 'Devon', 'comment' : 'hello'],
  ['first' : 'John', 'last' : 'Smith', 'comment' : 'bye', 'hidden' : true],
  ['first' : 'Joe', 'last' : 'Devon', 'comment' : 'world']
},
{
'id': $_id,
'user' : ['first_name' : 'Joe', 'last_name' : 'Shmoe' ],
'text' : 'meh',
'comment' : 
  ['first' : 'Joe', 'last' : 'Devon', 'comment' : 'sup'],
},
{
'id': $_id,
'user' : ['first_name' : 'Mr.', 'last_name' : 'Smith' ],
'text' : 'bah',
'comment' : 
  ['first' : 'Joe', 'last' : 'Devon', 'comment' : 'sup mon'],
}

我正在尝试运行一个查询,将返回所有内容,除了有"隐藏"的评论:true。

尝试了所有不起作用的方法。寻找一个可以工作的命令。请帮忙:)

这是目前mongodb不可能做到的,你将不得不在客户端过滤评论。

过滤机制只用于匹配或不匹配整个文档,然后检索其字段的子集,但不幸的是,您不能指定返回哪个字段的标准。

如果你有一个评论集合,你可以过滤掉那些有hidden: true

最新更新