0

First of all my code is obviously simplified. I create a new field named "followers", in order to store followers names, as array type.

USER SCHEMA

 var userSchema = new mongoose.Schema({
    local:{
        localId:{type:Number},
        username:{type:String},
        email:{type:String},          
        followers:{type:Array}       
    },
    facebook:{
        facebookId: {type:Number},     
        username:{type:String},
        email:{type:String},       
        followers:{type:Array}       
    },
    twitter:{
        twitterId: {type:Number},
        username:{type:String},
        email:{type:String},      
        followers:{type:Array} 
    }
});

DB record

{ "_id" : ObjectId("...."), "facebook" : { "facebookId" : 10215321125408144, "username" : "johnny", "email" : "[email protected]}, "__v" : 0 }

But something weird comes up. When i make a query:

User.findOne({$or: [
              {'local.username': 'johnny' },
           { 'facebook.username': 'johnny'  },
            { 'twitter.username': 'johnny'  },
  ]}, function(err, user) {
            if(err){console.log(err);} 
              if(!user){
                console.log('cannot find user');
              }
              if(user){
                console.log('usrInfo is : '+user);
              return res.send(JSON.stringify(user));
            }
        });

i get as a result:

usrInfo is : { _id: 5a60de744b85e14051e03e2e,
  __v: 0,
  twitter: { followers: [] },
  facebook: 
   { facebookId: .....,
     username: 'johnny',
     email: '[email protected]',
    .............,
     followers: [] },
  local: { followers: [] } }

I cannot understand why

 twitter: { followers: [] } and   local: { followers: [] }

are showing up. Is there any way to avoid this; Because i'm new in MongoDB i would appreciate any explanation why MongoDB does that. Thanks

1 Answer 1

1

This issue occurs because, according to the Mongoose documentation, fields in your schema with type Array implicitly have a default value of []. Try setting the default value to undefined and see if that resolves your problem:

var userSchema = new mongoose.Schema({
    local:{
        localId:{type:Number},
        username:{type:String},
        email:{type:String},
        followers:{type:Array, default: undefined}
    },
    facebook:{
        facebookId: {type:Number},
        username:{type:String},
        email:{type:String},
        followers:{type:Array, default: undefined}
    },
    twitter:{
        twitterId: {type:Number},
        username:{type:String},
        email:{type:String},
        followers:{type:Array, default: undefined}
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it works. But if you want to explain this a little bit more.. [ ] means empty. Why MongoDB returns an empty array on non empty query?
I'm not sure what you mean by "non-empty" in this context. In your specific example, do you mean "non-existent"? If so, it's because of the way your schema is defined. The default value for arrays is an empty array, so when you perform a query and receive the document, any arrays with that default value will be automatically included in your results as an empty array despite not existing in your document. This is likely Mongoose-specific functionality, provided so that you don't have to worry about whether or not an array exists to perform basic array ops like $push and $pull.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.