小编BNK*_*NKS的帖子

MongoDB,聚合 $match 不适用于 ObjectId

我有一个架构是:

var photoSchema = new mongoose.Schema({
    id: String,     // Unique ID identifying this photo
    file_name: String,
    date_time: {type: Date, default: Date.now},
    user_id: mongoose.Schema.Types.ObjectId, // The user id of the user who created the photo.
    comments: [commentSchema] // Comment objects representing the comments made on this photo.
});

var Photo = mongoose.model('Photo', photoSchema);
Run Code Online (Sandbox Code Playgroud)

我正在尝试查找评论数量最多的特定用户的照片。这是我所做的:

Photo.aggregate([
        {
            $project: {
                id: 1,
                file_name: 1,
                date_time: 1,
                user_id: 1,
                comments: 1,
                length: {$size: "$comments"}
            }
        },
        {
            $match: {
                user_id: mongoose.Schema.Types.ObjectId(request.params.id)
            }
        },
        { …
Run Code Online (Sandbox Code Playgroud)

javascript mongoose mongodb node.js

6
推荐指数
1
解决办法
5038
查看次数

标签 统计

javascript ×1

mongodb ×1

mongoose ×1

node.js ×1