javascript - Mongodb 聚合与客户端处理

标签 javascript node.js mongodb 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} 
            ] 
}

我主要想运行三个查询:

  1. 给我 bob 发表的所有评论
  2. 查找在写帖子的同一天发表的所有评论,即 comments.postDate = title.postDate
  3. 找到bob在写帖子的同一天发表的所有评论

我的问题如下:

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

最佳答案

这里的最佳做法可能是将您的多个问题“分解”为几个问题,如果不仅如此,一个问题的答案可能会有让你了解对方。

我也不太热衷于回答任何没有您尝试做的事情的例子。但话虽如此,“搬起石头砸自己的脚”,从设计方法来看,这些问题是合理的,所以我会回答。

第 1 点:“bob”的评论

标准 $unwind 并过滤结果。首先使用 $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" }
    }}
])

要点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" }
    }}

])

第 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" }
    }}

])

结果

老实说,尤其是如果您正在使用一些索引 来提供给这些操作的初始 $match 阶段,那么很明显,这将围绕尝试迭代此“运行环”在代码中。

至少这减少了返回的记录“通过线路”,因此网络流量较少。当然,一旦收到查询结果,后处理就更少(或没有)。

作为一般惯例,数据库服务器硬件的性能往往比“应用程序服务器”硬件高一个数量级。因此,一般情况再次表明,在服务器 上执行的任何操作都会运行得更快。

  • 聚合正确吗:"is"。并且有很长的路要走。您甚至很快就会得到一个光标

  • 如何进行您想要的查询:事实证明非常简单。在现实世界的代码中,我们从不“硬编码”它,我们动态地构建它。因此,添加条件和属性应该像所有普通数据操作代码一样简单。

所以我通常不会回答这种风格的问题。但是要说谢谢!请问?

关于javascript - Mongodb 聚合与客户端处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22555437/

相关文章:

javascript - React + useState + Array + Mutation = 错误

javascript - 通过 Django Channels 和 Websockets 向客户端推送实时更新

node.js - 使用 NODE JS 和 EXPRESS 在公共(public)互联网上 App.js 功能丢失

c++ - Mongo C++ 批量操作

javascript - 用 express.js 处理 Mongoose 连接的正确方法是什么?

javascript - 如何比较路径是否在同一个文件夹中

javascript - meteor .js 1.0 : How do I return an _id outside of ObjectId using this. _id?

windows - 'slc' 不是内部或外部命令,也不是可运行的程序或批处理文件

node.js - 无法理解如何将 connect-flash 与 Express 4.x 结合使用?

node.js - nodejs + mongoose - 如何在nodejs中使用forEach