如何查询MongoDB中的嵌入文档并返回结果子集?
我有这样一个数据结构:如何查询MongoDB中的嵌入文档并返回结果子集?,mongodb,lithium,Mongodb,Lithium,我有这样一个数据结构: 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' : tru
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'],
}
我正在尝试运行一个查询,该查询将返回除“hidden”注释之外的所有内容:true
尝试了所有不起作用的东西。正在寻找一个可以工作的命令。请提供帮助:)这在mongodb中目前无法实现,您必须在客户端过滤评论 过滤机制只用于匹配或不匹配整个文档,然后检索其字段的子集,但遗憾的是,您无法指定要返回哪些字段的条件
如果你有一个评论集合,你可以过滤掉那些有
隐藏:true
命令的评论?MongoDB命令行客户端?最终我会尝试在Limition中使用它,但我喜欢将问题分解为让查询在命令行上运行,因为我甚至无法做到这一点。记住,您只能查询顶级文档,这一点很重要。如果你需要你正在尝试做的事情,那么评论永远不应该被嵌入。要在某种程度上保留模式,您可以采取的唯一方法是创建一个“comment”和“hiddencoment”字段,并相应地填充它们。只需一次原子更新,就可以将注释从相应的数组移动到相应的数组。谢谢@RemonvanVliet。还是一个全新的Mongo。我不知道。现在我有了一个很好的工作规则来帮助决定模式。非常有帮助,不客气。祝你好运;)