These are chat archives for Automattic/mongoose

26th
Oct 2018
nibiralpha
@nibiralpha
Oct 26 2018 05:11

i am trying to build a chat application.. i am not sure how to structure my mongodb schema..

currently i am using this schema.. collection name: conversations

{
conversation: {
id: 1,
name: test
},
messages: [
{
_id: mongoid("123"),
userID: 1,
message: "test message"
date: date..,
status: 1,
pins: [2, 3] //message pin by user ids
mentions: [4, 5],
replyMsgID: mongoid(456)
},
...
],
users: [
{
userID: 1,
blocked: 1,
lastActiveTime: dateTime,
},
...
]
}
...
i am thinking another schema like this...

{
conversation: {
id: 1,
name: test
},
messages: [
{
_id: mongoid("999")
msgID: mongoID("123")
},
{
_id: mongoid("888")
msgID: mongoID("456")
},
...
],
users: [
{
userID: 1,
blocked: 1,
lastActiveTime: dateTime,
},
...
]
}
messages collection..

messages: [
{
_id: mongoid("123"),
userID: 1,
message: "test message"
date: date..,
status: 1,
pins: [2, 3] //message pin by user ids
mentions: [4, 5],
replyMsgID: mongoid(456),
conversationID: 1
},
...
]
i am new to mongodb and i am confused which schema should be use for a scalable for chat application..

i thinks the problems i will face with schema 1 are.. at some point the document size might get pass 16mb limit of mongo... when fetch or update anything like if i need only 1 message or just a single user the mongo will return the whole document with thousands of messages

the problem with 2nd schema ... i will need to join two collections with aggregate framework and might need to use $unwind, $group a lot... which i think will be very performance heavy... to create a new message i will have to insert the message in the messages collection first then insert the the newly added message to the conversations collections property messages field, same goes for delete.. so lots of query.. :|

it would be really helpful if someone could help me out to select a schema.. or create a better schema..

thanks in advance :)

SCHEMA 1

{
conversation: {
id: 1,
name: test
},
messages: [
{
_id: mongoid("123"),
userID: 1,
message: "test message"
date: date..,
status: 1,
pins: [2, 3] //message pin by user ids
mentions: [4, 5],
replyMsgID: mongoid(456)
},
...
],
users: [
{
userID: 1,
blocked: 1,
lastActiveTime: dateTime,
},
...
]
}
...
SCHEMA 2
{
conversation: {
id: 1,
name: test
},
messages: [
{
_id: mongoid("999")
msgID: mongoID("123")
},
{
_id: mongoid("888")
msgID: mongoID("456")
},
...
],
users: [
{
userID: 1,
blocked: 1,
lastActiveTime: dateTime,
},
...
]
}
MESSAGE COLLECTION
messages: [
{
_id: mongoid("123"),
userID: 1,
message: "test message"
date: date..,
status: 1,
pins: [2, 3] //message pin by user ids
mentions: [4, 5],
replyMsgID: mongoid(456),
conversationID: 1
},
...
]

Ghost
@ghost~5b53da7dd73408ce4fa1d2e5
Oct 26 2018 05:50

Hi, why I even set the date to be .toDatestring() it became different in the database.

const exercise = new Exercise(req.body); 
    exercise.username = result.username;  
    exercise.save((err, enteredExercise) => {
      if(err) { return next(err) };
      enteredExercise = enteredExercise.toObject();
      delete enteredExercise.__v;
      enteredExercise._id = enteredExercise.userId;
      delete enteredExercise.userId;
      enteredExercise.date = new Date(enteredExercise.date).toDateString()  // <--- HERE
      res.json(enteredExercise);
    })

Then in the database will be

    {
        "_id": "5bd2a7b54ca7576442a98283",
        "userId": "ByaoDPhBg",
        "duration": 12,
        "description": "log 12",
        "username": "hiee",
        "date": "1958-03-21T00:00:00.000Z",     // <--- I expect that it should be in this format"Fri Mar 21 1958"
        "__v": 0
    }

The schema is this

const exerciseSchema = new Schema({
  userId: {type: String},
  description: {type: String, required: true},
  duration: {type: Number, required: true},
  date: {type: Date}, // if no date specified, put the date now
  username: {type: String}
});
Hitesh Joshi
@evoxtorm
Oct 26 2018 10:57
@lineus Thanks for help
Ivan
@Teoreez
Oct 26 2018 12:32
Hey! I have problem with find a good example of querry with promise. Like Users.findOne().lean().then().exec() can you help me find one?
Grant Allor
@gallor
Oct 26 2018 16:08
Hey guys, just following up on my previous post. Does anyone know how to get some insight into why Mongoose can't connect to some nodes of a replica set? There doesn't appear to be anything wrong from the networking side of things so needing to get more insight from the Mongoose side of things. Or would this be a Node question? Thank!