anv*_*rik 2 javascript mongodb node.js aggregation-framework
我有一个blogs几乎有以下架构的集合:
{
title: { name: "My First Blog Post",
postDate: "01-28-11" },
content: "Here is my super long post ...",
comments: [ { text: "This post sucks!"
, name: "seanhess"
, created: 01-28-14}
, { text: "I know! I wish it were longer"
, name: "bob"
, postDate: 01-28-11}
]
}
Run Code Online (Sandbox Code Playgroud)
我主要想运行三个查询:
comments的制作只 bobcomments邮件写入的同一天制作的comments.postDate = title.postDate.comments制作的bob内容我的问题如下:
db.blogs.find({"comments.name":"bob"}, {comments.name:1, comments.postDate:1, title.postDate:1}),然后进行客户端后处理以循环返回结果.这是个好主意吗?我想请注意,这可能会返回几千个文件.这里最好的做法是将你的多个问题"分解" 成几个问题,如果不仅仅是因为这个问题,一个问题的答案可能会让你理解另一个问题.
我也不是很热衷于回答任何没有显示你试图做什么的例子的事情.但随着这一点和"射击自己的脚",从设计方法的问题是合理的,所以我会回答.
标准$展开并过滤结果.首先使用$ match,这样就不会处理不需要的文档.
db.collection.aggregate([
// Match to "narrow down" the documents.
{ "$match": { "comments.name": "bob" }},
// Unwind the array
{ "$unwind": "$comments" },
// Match and "filter" just the "bob" comments
{ "$match": { "comments.name": "bob" }},
// Possibly wind back the array
{ "$group": {
"_id": "$_id",
"title": { "$first": "$title" },
"content": { "$first": "$content" },
"comments": { "$push": "$comments" }
}}
])
Run Code Online (Sandbox Code Playgroud)
db.collection.aggregate([
// Try and match posts within a date or range
// { "$match": { "title.postDate": Date( /* something */ ) }},
// Unwind the array
{ "$unwind": "$comments" },
// Aha! Project out the same day. Not the time-stamp.
{ "$project": {
"title": 1,
"content": 1,
"comments": 1,
"same": { "$eq": [
{
"year" : { "$year": "$title.postDate" },
"month" : { "$month": "$title.postDate" },
"day": { "$dayOfMonth": "$title.postDate" }
},
{
"year" : { "$year": "$comments.postDate" },
"month" : { "$month": "$comments.postDate" },
"day": { "$dayOfMonth": "$comments.postDate" }
}
]}
}},
// Match the things on the "same
{ "$match": { "same": true } },
// Possibly wind back the array
{ "$group": {
"_id": "$_id",
"title": { "$first": "$title" },
"content": { "$first": "$content" },
"comments": { "$push": "$comments" }
}}
])
Run Code Online (Sandbox Code Playgroud)
db.collection.aggregate([
// Try and match posts within a date or range
// { "$match": { "title.postDate": Date( /* something */ ) }},
// Unwind the array
{ "$unwind": "$comments" },
// Aha! Project out the same day. Not the time-stamp.
{ "$project": {
"title": 1,
"content": 1,
"comments": 1,
"same": { "$eq": [
{
"year" : { "$year": "$title.postDate" },
"month" : { "$month": "$title.postDate" },
"day": { "$dayOfMonth": "$title.postDate" }
},
{
"year" : { "$year": "$comments.postDate" },
"month" : { "$month": "$comments.postDate" },
"day": { "$dayOfMonth": "$comments.postDate" }
}
]}
}},
// Match the things on the "same" field
{ "$match": { "same": true, "comments.name": "bob" } },
// Possibly wind back the array
{ "$group": {
"_id": "$_id",
"title": { "$first": "$title" },
"content": { "$first": "$content" },
"comments": { "$push": "$comments" }
}}
])
Run Code Online (Sandbox Code Playgroud)
老实说,特别是如果你使用一些索引来提供这些操作的初始$ match阶段,那么应该非常清楚,这将在试图在代码中迭代这个"运行环".
在最起码这样可以减少返回的记录"过线",所以减少网络流量.当然,一旦收到查询结果,发布流程的次数就会减少(或没有).
作为一般惯例,数据库服务器硬件的性能往往比"应用服务器"硬件高一个数量级.因此,一般条件是在服务器上执行的任何操作都会运行得更快.
聚合是正确的:"是".并且很长一段路.你很快就会得到一个光标.
你怎么做你想要的查询:显示非常简单.在现实世界的代码中,我们从不"硬编码",我们动态地构建它.因此,添加条件和属性应该与所有常规数据操作代码一样简单.
所以我通常不会回答这个风格问题.但是说谢谢!请 ?