Must a new Document in MongoDB be exactly the same as the Schema?

I am facing problems in the creation of new Documents in MongoDB using Mongoose.

Must the new Documents be exactly the same as the Schemas?

I mean I have the following Schema:

ar userSchema = new Schema({

        userID: Number,
        userName: String,
        userEmail: String,
        teams:Array,
        fbUID: String,
        googleUID: String,
        twitter: String 


});

But to create a new user Document I dont have all the fields, so my Document will not contain all the field as the Schema above.

For example, I am adding a new document based on the Schema above. My Doc is:

var users = mongoose.model('users',userSchema);

 var user = new users({
        userID: id, //give the id of the next user in Dbase
        userName: userName, 
        userEmail: 'userEmail',
        teams:[],
        fbUID: '1234'
     });

user.save(function(err, user){
        if(err) return console.error(err);
        log.d("user salved", user);
     });

So, I dont have all the fields, and I could not save any Doc so far.

Does it matter if I don't have all the fields in the Schema in the new Document?

UPDATE:

and the second problem is that I am getting one of these :

 fbUID,
        googleUID,
        twitter

through a function, and I don't know which of those I am receiving, so I am trying to save the Document this way:

function salveUser(userName, socialMediaType, socialMediaID){

    var id;



      users.count(function(err, count){
        if(err){
            log.d("Err in counting files")
        }
        id = count;

      }); 

     var user = new users({
        userID: id, //give the id of the next user in Dbase
        userName: userName, 
        userEmail: 'userEmail',
        teams:[],
        socialMediaType : socialMediaID
     });


     user.save(function(err, user){
        if(err) return console.error(err);
        log.d("user salved", user);
     });

     currentSession =  sessionOBJ.login(id, socialMediaID);  

     return currentSession;
}

}

That is why I would like to know if Mongoose switch the key in the Document by the word I am receiving in my function or if it uses the exactly word I am putting there, in this case "socialMediaType".

Does someone know?

It looks like you're code is fine as Leonid said in the comments

I would try saving a user with all direct values to rule out any variable not being set....

So try this:

var user = new users({
    userID: 11, //give the id of the next user in Dbase
    userName: 'John', 
    userEmail: 'John@john.com',
    teams:['Blue', 'Green'],
    fbUID: '1234'
 });

If that works then the problem is probably that the id or userName variables haven't been set before actually hitting this message.

You don't have to put values for all fields just because they are in the model.

Best of luck!

Answering your main question: no, new Documents shouldn't be exactly the same as the Schemas.

Actually, none of the document's fields defined in its schema are required in order to save it, unless you explicitly marked some of them as required.

But you made several mistakes in your salveUser function.


First, your id = count assignment will take place long after user document will be created and saved, because users.count is asynchronous. It means that userID in your example will always be udefined.

To fix it you should wait for users.count to finish before trying to use it's results:

users.count(function(err, count){
  if(err){
    return log.d("Err in counting files")
  }

  var user = new users({
    userID: count, //give the id of the next user in Dbase
    userName: userName
    /* other  fields */
  });

  user.save(/* your callback */);
}); 

Second, it looks like you're trying to assign a value socialMediaID to a field with a name equal to socialMediaType value. But you can't do it the way you're trying ti do it. So, instead of setting fbUID/googleUID field you're trying to set an unexisting socialMediaType field.

To fix it you should use [] assignment:

var user = new users({
  userID: id, //give the id of the next user in Dbase
  userName: userName, 
  userEmail: 'userEmail'
});
user[socialMediaType] = socialMediaID;

Since you don't have a unique index on userID field an since MongoDB automatically assigns an unique _id to all documents, you should be able to successfully .save() your document.


But in your question you said that you're unable to do it, meaning you have some other problem in your code. Please, post an exact exception you're getting from mongoose in .save() callback.