Mongodb聚合与客户端处理

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)

我主要想运行三个查询:

  1. 给我所有comments的制作 bob
  2. 找到所有在comments邮件写入的同一天制作的comments.postDate = title.postDate.
  3. 在撰写帖子的同一天查找所有comments制作的bob内容

我的问题如下:

  • 这三个将是非常频繁的查询,因此使用聚合框架是一个好主意吗?
  • 对于第三个查询,我可以简单地进行查询db.blogs.find({"comments.name":"bob"}, {comments.name:1, comments.postDate:1, title.postDate:1}),然后进行客户端后处理以循环返回结果.这是个好主意吗?我想请注意,这可能会返回几千个文件.
  • 如果你能提出一些方法来进行第三次查询,我将很高兴.

Nei*_*unn 7

这里最好的做法是将你的多个问题"分解" 成几个问题,如果不仅仅是因为这个问题,一个问题的答案可能会让你理解另一个问题.

也不是很热衷于回答任何没有显示你试图做什么的例子的事情.但随着这一点和"射击自己的脚",从设计方法的问题是合理的,所以我会回答.

第1点:"bob"的评论

标准$展开并过滤结果.首先使用$ 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)

第2点:同一天的所有评论

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)

第3点:同一天的"bob"

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阶段,那么应该非常清楚,这将在试图在代码中迭代这个"运行环".

最起码这样可以减少返回的记录"过线",所以减少网络流量.当然,一旦收到查询结果,发布流程的次数就会减少(或没有).

作为一般惯例,数据库服务器硬件的性能往往比"应用服务器"硬件高一个数量级.因此,一般条件是在服务器上执行的任何操作都会运行得更快.

  • 聚合是正确的:"是".并且很长一段路.你很快就会得到一个光标.

  • 你怎么做你想要的查询:显示非常简单.在现实世界的代码中,我们从不"硬编码",我们动态地构建它.因此,添加条件和属性应该与所有常规数据操作代码一样简单.

所以我通常不会回答这个风格问题.但是说谢谢!请 ?