Monday, 17 April 2017

mongoose subdocument retrieved as String instead of object

So, I have the following objects

var Sub = {
  name: String,
};

var UserSchema = new mongoose.Schema({
  name: String,
  sub: Sub
});

module.exports = mongoose.model('User', UserSchema);

In the database I have the following:

db.users.find({}).pretty(); { "_id" : ObjectId("xyz"), "name" : "John", "sub" : { "name" : "Sub Name" } }

Now, when I query for a user with name "John", the "sub" sub-document is retrieved as a string

typeof user.sub === 'string'

To perform the query I am using User.findById method

User.findById("xyz", function(err, user){
  console.log(typeof user.sub);
})

Any clues what is going on here?



via Joe.b

No comments:

Post a Comment