如何在mongodb上实现聚合方法(管道)?

如何在mongodb上实现聚合方法(管道)?,mongodb,nosql,aggregate,Mongodb,Nosql,Aggregate,我需要在mongodb中执行以下操作: 使用聚合方法创建一个管道,该管道: 为包含数量的每个文档添加新字段categoriesCount 它有哪些类别 至少使用两个类别筛选书籍 按标题订购 下面是我的json文件的一个示例: { "_id": 1, "title": "Unlocking Android", "isbn": "1933988673", "pageCount": 416, "publishedDate": { "$date": "2009-04-01T00:00:00.00

我需要在mongodb中执行以下操作:

使用聚合方法创建一个管道,该管道:

为包含数量的每个文档添加新字段categoriesCount 它有哪些类别 至少使用两个类别筛选书籍 按标题订购 下面是我的json文件的一个示例:

{
"_id": 1,
"title": "Unlocking Android",
"isbn": "1933988673",
"pageCount": 416,
"publishedDate": {
    "$date": "2009-04-01T00:00:00.000-0700"
},
"thumbnailUrl": "https://s3.amazonaws.com/AKIAJC5RLADLUMVRPFDQ.book-thumb-images/ableson.jpg",
"shortDescription": "Unlocking Android: A Developer's Guide provides concise, hands-on instruction for the Android operating system and development tools. This book teaches important architectural concepts in a straightforward writing style and builds on this with practical and useful examples throughout.",
"longDescription": "Android is an open source mobile phone platform based on the Linux operating system and developed by the Open Handset Alliance, a consortium of over 30 hardware, software and telecom companies that focus on open standards for mobile devices. Led by search giant, Google, Android is designed to deliver a better and more open and cost effective mobile experience.    Unlocking Android: A Developer's Guide provides concise, hands-on instruction for the Android operating system and development tools. This book teaches important architectural concepts in a straightforward writing style and builds on this with practical and useful examples throughout. Based on his mobile development experience and his deep knowledge of the arcane Android technical documentation, the author conveys the know-how you need to develop practical applications that build upon or replace any of Androids features, however small.    Unlocking Android: A Developer's Guide prepares the reader to embrace the platform in easy-to-understand language and builds on this foundation with re-usable Java code examples. It is ideal for corporate and hobbyists alike who have an interest, or a mandate, to deliver software functionality for cell phones.    WHAT'S INSIDE:        * Android's place in the market      * Using the Eclipse environment for Android development      * The Intents - how and why they are used      * Application classes:            o Activity            o Service            o IntentReceiver       * User interface design      * Using the ContentProvider to manage data      * Persisting data with the SQLite database      * Networking examples      * Telephony applications      * Notification methods      * OpenGL, animation & multimedia      * Sample Applications  ",
"status": "PUBLISH",
"authors": [
    "W. Frank Ableson",
    "Charlie Collins",
    "Robi Sen"
],
"categories": [
    "Open Source",
    "Mobile"
]
}
您可以使用$size,它返回数组的大小。 下面的聚合查询为您提供了所需的内容

它添加了一个新字段,表示“类别”的大小 基于大于或等于2的大小的筛选器 使用project删除新添加的字段。 根据标题对结果进行排序
到目前为止你尝试了什么?我还得数一数每个类别有多少本书。我所做的是:db.Books.aggregate[{$unwind:$categories},{$group:{{{u id:$categories,Cantidad:{$sum:1}},}],这很好。但是我不知道如何做第一部分:为每个文档添加一个字段。。。
db.collection.aggregate([
    {
        $project : {"newField" : {$size : "$categories"}, title : 1, isbn: 1, pageCount: 1, publishedDate: 1, thumbnailUrl:1,
        shortDescription: 1, longDescription: 1, status: 1, authors: 1, categories: 1}

    },
    {
        $match : {"newField" : {$gte: 2}}
    },
    {
        $project : {newField : 0}
    },
    {
        $sort : {title : 1}
    }
])