Mongodb聚合查询数组的大小

时间:2016-06-17 11:52:11

标签: mongodb mongoose

我为User schema编写了一个mongoose模型。用于服务器端的hapi.js框架。这个查询存在一些问题。

 User.aggregate([{"$project":{"email":1,"count":{"$size":"$followers"}}}],function(err,res){

                    if(err)
                            console.log(err);
                reply(res);
    }); 

以上查询给出的错误如下所示:

{ [MongoError: exception: The argument to $size must be an Array, but was of type: String]
  name: 'MongoError',
  message: 'exception: The argument to $size must be an Array, but was of type: String',
  errmsg: 'exception: The argument to $size must be an Array, but was of type: String',
  code: 17124,
  ok: 0 }

我的猫鼬模特:

let UserSchema = new Schema({
    username:{type:String,required:true,unique:true},
    name:{type:String,required:true},
    email:{type:String,required:true,unique:true},
    password:{type:String,required:true},
    created_at:{type:Date,default:new Date()},
    modified_at:{type:Date},
    profile_image:{type:String},
    description:{type:String,default:null},
    favourites_count:{type:Number,default:0},
    follow_request_sent:{type:Boolean},
    sex:{type:String,enum:['M','F']},
    followers:[{type:String,unique:true}],
    following:[{type:String,unique:true}],
    followers_count:{type:Number,default:0},
    geo_enabled:{type:Boolean,default:false},
    id:{type:Number},
    location: {
            'type': {type: String, enum: constants.GEO_JSON_TYPES.Point, default: constants.GEO_JSON_TYPES.Point},
             coordinates: {type: [Number], default: [0, 0]}
        },
    status_count:{type:Number,default:0},
    isDeleted:{type:Boolean,default:false},
    isActive:{type:Boolean,default:true},
    isVerified:{type:Boolean,default:false},
    timezone:{type:String,default:moment().tz('Asia/Kolkata').format()} 
});

0 个答案:

没有答案