Suppose I have a user Schema as below
var User = new db.Schema({
username : {type: String, unique: true}
, password : String
, email: String
});
To accommodate the user having friends I’m thinking of modifying the schema as follows
var User = new db.Schema({
username : {type: String, unique: true}
, password : String
, email: String
, friends: [?]
});
Rather than just storing ObjectIds I’m thinking of denormalizing the database for faster queries. Suppose for now I just wish to store ObjectId and username in the friends array. Is it possible to do something like
friends: [{String, String}]
where the two Strings represent ObjectId and username? I don’t want to create a brand new schema since I don’t need a new ObjectId.
Sure you can do this, but I would really advice to name the fields, like (in Mongo shell syntax):
However, you (of course) don’t have to store the ObjectId at all, as your
usernameis already unique. Heck, you could even make a user’s username as ID for the_idfield:I think in Mongoose, you’ll have to do this like (from reading “Defining documents within documents” at http://mongoosejs.com/docs/model-definition.html)