我的数据库结构如下:
{ title : "My First Post", author: "Jane",
comments : [{ by: "Abe", text: "First" },
{ by : "Ada", text : "Good post" } ]
}
Run Code Online (Sandbox Code Playgroud)
我无法写出以下内容:
post变量中后,显示Abe的评论文本谢谢.
获取Ada 未评论的所有帖子
在MongoDB中有一个$ nin(代表不在)运算符,您可以像这样编写查询:
db.my.collection.find({'comments.by': {$nin: ['Ada']}});
Run Code Online (Sandbox Code Playgroud)
获取Abe评论的所有帖子"First"
就注释存储在数组中而言,通过索引访问特定项目是绝对合法的.要实现此目标,您可以编写以下查询:
db.my.collection.find({'comments.0.by': 'Abe'});
Run Code Online (Sandbox Code Playgroud)
找到帖子并将其存储在post变量中后,显示Abe的评论文本
这取决于您的客户端应用程序如何呈现检索到的数据.
| 归档时间: |
|
| 查看次数: |
924 次 |
| 最近记录: |