MongoDB获得嵌套文档的总数

时间:2017-04-24 14:46:47

标签: mongodb aggregation-framework

我在MongoDB中有以下文档。我想添加一个新的临时字段作为" total_comments"在每个文件中。如果文档没有注释,则它显示" total_comments":0否则显示为" total_comments":n,其中n表示对象数组中的注释总数。我尝试使用MongoDB聚合查询,但我无法获得所需的结果。

  

模式

    var photoSchema = new Schema({
    user_id: {
        ref: 'User',
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        index: true
    },
    album_id: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Album',
        require: false,
        index: true
    },
    photo_name: {
        type: String,
        required: true
    },
    comments: [{
            user_id: {
                type: Schema.Types.ObjectId,
                ref: 'User'
            },
            comment: [],
            like: [{
                    type: Schema.Types.ObjectId,
                    ref: 'User'
                }],
            is_delete: {
                type: Number,
                default: 0,
                index: true
            }, // 0: insert, 1: delete
            created_at: {
                type: Date,
                default: Date.now,
                index: true
            }, // created date
            reply_reference_id: {
                type: Schema.Types.ObjectId
            }
        }],
    is_publish: {
        type: Number,
        default: 0
    },
    is_visible: {
        type: Number,
        default: 0
    },
    is_active: {
        type: Number,
        default: 1
    },
    is_delete: {
        type: Number,
        default: 0
    },
    created_at: {
        type: Date,
        default: Date.now
    },
    updated_at: {
        type: Date
    }
}, {collection: 'photo'});
  

当前文件

{
    "_id" : ObjectId("58ef5aeb6089260c9efed2d7"),
    "user_id" : ObjectId("57554e7e64c21f560ce002f1"),
    "album_id" : ObjectId("587f6e4e424085443af9e668"),
    "photo_name" : "11425155111184.jpeg",
    "comments":[
        {
            "user_id" : ObjectId("572c80a2220d1d4028437058"),
            "_id" : ObjectId("58edc650acde74110ceec9e2"),           
            "comment" : [{"text" : "comment1"}]
        },
        {
            "user_id" : ObjectId("572c80a2220d1d4028437058"),
            "_id" : ObjectId("58edc650acde74110ceec9e1"),           
            "comment" : [{"text" : "comment2"}]
        },
        {
            "user_id" : ObjectId("572c80a2220d1d4028437058"),
            "_id" : ObjectId("58edc650acde74110ceec9e4"),           
            "comment" : [{"text" : "comment3"}]
        },      
        {
            "user_id" : ObjectId("572c80a2220d1d4028437058"),
            "_id" : ObjectId("58edc650acde74110ceec9e7"),           
            "comment" : [{"text" : "comment4"}]
        },
        {
            "user_id" : ObjectId("572c80a2220d1d4028437058"),
            "_id" : ObjectId("58edc650acde74110ceec9e8"),           
            "comment" : [{"text" : "comment5"}]
        },
    ]
},
{
    "_id" : ObjectId("58ef5aeb6089260c9efed2d6"),
    "user_id" : ObjectId("57554e7e64c21f560ce002f1"),
    "album_id" : ObjectId("587f6e4e424085443af9e668"),
    "photo_name" : "11425155111184.jpeg",
    "comments":[
        {
            "user_id" : ObjectId("572c80a2220d1d4028437028"),
            "_id" : ObjectId("58edc650acde74110ceec9e2"),           
            "comment" : [{"text" : "comment1"}]
        },
        {
            "user_id" : ObjectId("572c80a2220d1d4028437018"),
            "_id" : ObjectId("58edc650acde74110ceec9e1"),           
            "comment" : [{"text" : "comment2"}]
        },
        {
            "user_id" : ObjectId("572c80a2220d1d4028437048"),
            "_id" : ObjectId("58edc650acde74110ceec9e4"),           
            "comment" : [{"text" : "comment3"}]
        },      
        {
            "user_id" : ObjectId("572c80a2220d1d4028437078"),
            "_id" : ObjectId("58edc650acde74110ceec9e7"),           
            "comment" : [{"text" : "comment4"}]
        },
        {
            "user_id" : ObjectId("572c80a2220d1d4028437098"),
            "_id" : ObjectId("58edc650acde74110ceec9e8"),           
            "comment" : [{"text" : "comment5"}]
        },
    ]
},
{
    "_id" : ObjectId("58ef5aeb6089260c9efe72d6"),
    "user_id" : ObjectId("57554e7e64c21f5608e002f1"),
    "album_id" : ObjectId("587f6e4e424085443af9e668"),
    "photo_name" : "11425155111184.jpeg",
    "comments":[
        {
            "user_id" : ObjectId("572c80a2220d1d4028447028"),
            "_id" : ObjectId("58edc650acde74110ceec9e2"),           
            "comment" : [{"text" : "comment1"}]
        },
        {
            "user_id" : ObjectId("572c80a2220d1d4028435018"),
            "_id" : ObjectId("58edc650acde74110ceec9e1"),           
            "comment" : [{"text" : "comment2"}]
        }
    ]
},
{
    "_id" : ObjectId("58ef5aeb60898960c9efe72d6"),
    "user_id" : ObjectId("57554e7e64c21f5608e002f1"),
    "album_id" : ObjectId("587f6e4e424085443af9e668"),
    "photo_name" : "11425155111183.jpeg",
    "comments":[]
},
{
    "_id" : ObjectId("58ef588b6089260c9efe72d6"),
    "user_id" : ObjectId("57554e7e64c21f5608e002f1"),
    "album_id" : ObjectId("587f6e4e424085443af9e668"),
    "photo_name" : "11425155111174.jpeg",
    "comments":[]
}
  

预期结果

{
    "_id" : ObjectId("58ef5aeb6089260c9efed2d7"),
    "user_id" : ObjectId("57554e7e64c21f560ce002f1"),
    "album_id" : ObjectId("587f6e4e424085443af9e668"),
    "photo_name" : "11425155111184.jpeg",
    "total_comments": 5,
    "comments":[
        {
            "user_id" : ObjectId("572c80a2220d1d4028437058"),
            "_id" : ObjectId("58edc650acde74110ceec9e2"),           
            "comment" : [{"text" : "comment1"}]
        },
        {
            "user_id" : ObjectId("572c80a2220d1d4028437058"),
            "_id" : ObjectId("58edc650acde74110ceec9e1"),           
            "comment" : [{"text" : "comment2"}]
        },
        {
            "user_id" : ObjectId("572c80a2220d1d4028437058"),
            "_id" : ObjectId("58edc650acde74110ceec9e4"),           
            "comment" : [{"text" : "comment3"}]
        },      
        {
            "user_id" : ObjectId("572c80a2220d1d4028437058"),
            "_id" : ObjectId("58edc650acde74110ceec9e7"),           
            "comment" : [{"text" : "comment4"}]
        },
        {
            "user_id" : ObjectId("572c80a2220d1d4028437058"),
            "_id" : ObjectId("58edc650acde74110ceec9e8"),           
            "comment" : [{"text" : "comment5"}]
        },
    ]
},
{
    "_id" : ObjectId("58ef5aeb6089260c9efed2d6"),
    "user_id" : ObjectId("57554e7e64c21f560ce002f1"),
    "album_id" : ObjectId("587f6e4e424085443af9e668"),
    "photo_name" : "11425155111184.jpeg",
    "total_comments": 5,
    "comments":[
        {
            "user_id" : ObjectId("572c80a2220d1d4028437028"),
            "_id" : ObjectId("58edc650acde74110ceec9e2"),           
            "comment" : [{"text" : "comment1"}]
        },
        {
            "user_id" : ObjectId("572c80a2220d1d4028437018"),
            "_id" : ObjectId("58edc650acde74110ceec9e1"),           
            "comment" : [{"text" : "comment2"}]
        },
        {
            "user_id" : ObjectId("572c80a2220d1d4028437048"),
            "_id" : ObjectId("58edc650acde74110ceec9e4"),           
            "comment" : [{"text" : "comment3"}]
        },      
        {
            "user_id" : ObjectId("572c80a2220d1d4028437078"),
            "_id" : ObjectId("58edc650acde74110ceec9e7"),           
            "comment" : [{"text" : "comment4"}]
        },
        {
            "user_id" : ObjectId("572c80a2220d1d4028437098"),
            "_id" : ObjectId("58edc650acde74110ceec9e8"),           
            "comment" : [{"text" : "comment5"}]
        },
    ]
},
{
    "_id" : ObjectId("58ef5aeb6089260c9efe72d6"),
    "user_id" : ObjectId("57554e7e64c21f5608e002f1"),
    "album_id" : ObjectId("587f6e4e424085443af9e668"),
    "photo_name" : "11425155111184.jpeg",
    "total_comments": 2,
    "comments":[
        {
            "user_id" : ObjectId("572c80a2220d1d4028447028"),
            "_id" : ObjectId("58edc650acde74110ceec9e2"),           
            "comment" : [{"text" : "comment1"}]
        },
        {
            "user_id" : ObjectId("572c80a2220d1d4028435018"),
            "_id" : ObjectId("58edc650acde74110ceec9e1"),           
            "comment" : [{"text" : "comment2"}]
        }
    ]
},
{
    "_id" : ObjectId("58ef5aeb60898960c9efe72d6"),
    "user_id" : ObjectId("57554e7e64c21f5608e002f1"),
    "album_id" : ObjectId("587f6e4e424085443af9e668"),
    "photo_name" : "11425155111183.jpeg",
    "total_comments": 0,
    "comments":[]
},
{
    "_id" : ObjectId("58ef588b6089260c9efe72d6"),
    "user_id" : ObjectId("57554e7e64c21f5608e002f1"),
    "album_id" : ObjectId("587f6e4e424085443af9e668"),
    "photo_name" : "11425155111174.jpeg",
    "total_comments": 0,
    "comments":[]
}

当我执行聚合查询时,每次都会抛出以下错误。

assert: command failed: {
    "ok" : 0,
    "errmsg" : "The argument to $size must be an Array, but was of type: EOO",
    "code" : 17124
} : aggregate failed
Error: command failed: {
    "ok" : 0,
    "errmsg" : "The argument to $size must be an Array, but was of type: EOO",
    "code" : 17124
} : aggregate failed
    at Error (<anonymous>)
    at doassert (src/mongo/shell/assert.js:11:14)
    at Function.assert.commandWorked (src/mongo/shell/assert.js:244:5)
    at DBCollection.aggregate (src/mongo/shell/collection.js:1149:12)
    at (shell):1:10
2017-04-25T11:07:31.198+0530 Error: command failed: {
    "ok" : 0,
    "errmsg" : "The argument to $size must be an Array, but was of type: EOO",
    "code" : 17124
} : aggregate failed at src/mongo/shell/assert.js:13

我在其他系列中使用的相同类型的架构,但它们工作正常,但我不知道为什么这不适用于此集合。

1 个答案:

答案 0 :(得分:0)

您可以将$ project与$ size一起使用:

db.collectionName.aggregate([
    {
        $project: {
            user_id: 1,
            album_id: 1,
            photo_name: 1,
            total_comments: { $size: "$comments" },
            comments: 1
        }
    }
])