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

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

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

  • 为每个文档添加新的字段类别数量以及数量 拥有的类别
  • 过滤至少具有两个类别的图书
  • 按标题排序

这是我的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"
]
}
cd773213220 回答:如何在mongodb上实现聚合方法(管道)?

您可以使用$size来返回数组的大小。 下面的聚合查询可为您提供所需的信息。

  1. 它添加了一个新字段,用于表示“类别”的大小
  2. 基于大小大于或等于2的过滤器
  3. 使用项目删除新添加的字段。
  4. 根据“标题”对结果进行排序
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}
    }
])
本文链接:https://www.f2er.com/3039856.html

大家都在问