Tuesday, 25 April 2017

Using mongoose for a chatrooms

I need to create a Chat that contains multiple rooms, this is the schema for a single room:

var messageSchema = mongoose.Schema({
  user : String,
  content: String
});

var roomSchema = mongoose.Schema({
  roomNumber : Number,
  message : [messageSchema]
});

I need to create a collection "Rooms" that wraps all the rooms that I've created and I need to associate each of this room to a number.

In Javascript, I would have used :

rooms = new Array(10).fill().map(function(){
 return new room();
});

And then by typing rooms[2], I would have accessed the room number 2.

How do I make that using mongoose please?



via Mit

No comments:

Post a Comment