Mongoose searching on array subdocument ObjectId not working

This is honestly driving me crazy. Here's the problem:

I'm running the following query in Mongoose:

s.findSubdocument=function(uid, cb)
{
     this.model('User').find({"flwng._id":uid.toString()}).select('flwng').exec(cb);
}

on the following User Schema:

var userSchema= new mongoose.Schema(
    {
        uname:{type:String, index:{sparse:true, unique:true, dropDups:true}}, //the username
        email:String,
        pwd:{type:String},
        flwng:[{_id : {type : mongoose.Schema.ObjectId},uname : String}], //_id of users I am following
        flwrsz:Number,
        flwngsz:Number,
        feedsz:Number,
    }, {j: 1});//j:1 means guarantee it is written to the journal.

userSchema.set('autoIndex', true);
userSchema.index({"fid":1}, {sparse:true, unique:true, dropDups:true});
userSchema.index({"flwng._id" : 1});

Where uid="53c4f16c431247694f0000a3" ==> but I get an empty array back :(

When I run the same exact query in the mongo shell:

db.users.find({"flwng._id":"53c4f16c431247694f0000a3"});

I get the right set of results back. I tried with and without an index and schema on "flwng._id", I tried to drop the index, reIndex and I'm now running out of ideas. Am I doing something wrong with Mongoose?

Any help would be appreciated - thanks! Henri

There's a mismatch between your existing documents in mongodb and your schema. Your data has records where flwng._id is a String, which is why you get results in the mongo shell. But your mongoose schema defines that as an ObjectId, so when you query with mongoose, mongoose casts your string value to an ObjectId and the query doesn't match. Either write a migration to fix your existing data or update your schema to match your data in terms of String vs ObjectId data type and things should start working through mongoose.