MongoDB 查询列表中的对象属性
假设我确实有一个这样的文档:
{
_id: "cow",
comments: [
{user: "karl", comment: "I like cows.", at: /*a date*/},
{user: "harry", comment: "Cows are the best.", at: /*a date*/}
]
}
现在我想收集 "karl"
在我的数据库中留下的所有评论。或者只是特定日期的那些。 “comments”
已建立索引,我该如何查询?
Let's say I do have a document like this:
{
_id: "cow",
comments: [
{user: "karl", comment: "I like cows.", at: /*a date*/},
{user: "harry", comment: "Cows are the best.", at: /*a date*/}
]
}
Now I want to collect all the comments that "karl"
left in my db. Or just the ones on a certain date. The "comments"
are indexed, how can I query for that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
比如:
还没有测试过,但你明白了;您可以深入研究此类项目。
我建议您浏览此网站,因为它会引导您完成大量交互式教程:交互式教程
Something like:
Haven't tested but you get the idea; you can drill down on items as such.
I'd recommend going through this site as it drives you through a great deal of interactive tutorials: Interactive Tutorial
您应该使用 $elemMatch 来匹配同一文档中的多个条件。
类似于:
请参阅此处了解更多详细信息。
You should use $elemMatch to match more than one conditions in same doc.
Something like:
See here for more details.