what would be a good database schema for an extremely simple chat in NoSQL?
Asked Answered
C

2

7

There's a good existing answer on a basic schema in SQL.

I can understand it, it's very straightforward. We have a user table, a chat table, and a chat_line table (which in a sane world would be called messages).

I'm rather new to NoSQL, and my mind is still used to "normal" SQL schemes, and I'm trying to understand what would be the correct schema for a chat app in NoSQL (like mongo, or whathaveyou).

I'm talking the simplest form, between one user to another, nothing special - no file messages, no pictures, no group chats. Just text.

Carmen answered 23/10, 2018 at 23:16 Comment(3)
There is no such thing as nosql. nosql is the set of databases that are not sql. i.e. all of the rest. So witch one. If you know about 1,2,3rd normal form, then you can apply it to any data, see also CAP theory, and consistency models.Mandamandaean
Depends what you want to model.Mandamandaean
NoSQL spans a lot of different databases, can we assume you want to use something like Mongo with a JSON format? Based on that I can provide you with an answerTheogony
R
4

As others have pointed out NoSQL is a generic term that refers to any alternative to traditional relational databases in which data is placed in tables and data schema is carefully designed before the database is built.

You mentioned Mongo in your question... MongoDB is schema-less. What you can do is create your own class that interacts with an instance of a Mongo Database and in that class you define rules that the data needs to adhere to.

If you are using node.js, you can install Mongoose which allows you to interact with database in object oriented style by providing a straight-forward, schema-based solution to model your data.

Here is a very simple example on how you would define a chat schema in Mongoose, it is not meant to be a complete schema, it is just a start which hopefully will get you going in implementing what you need:

var chatSchema = new Schema({
    chatSession: { type: Number, index: true },    
    user: { type: String, default: 'anonymous' },
    chatLineText: { type: String },
    dateTime: { type: Date, default: Date.now },
});

var chatModel = mongoose.model('Chat', chatSchema);
var chatLine1 = new chatModel({
    chatSession: '2133123',
    user: 'someUserName',
    chatLineText: 'Hello yuvi!'
});

chatLine1.save(function (err, chatLine) {
    if (err) console.log(err);
    else console.log('following chatLine was saved:', chatLine);
Receivership answered 11/12, 2018 at 15:44 Comment(5)
Here is a tutorial on how to make a chat application using MongoDB + Mongoose: dzone.com/articles/…Receivership
Thank you for the detailed explanation. As I said, NoSQL is new to me so I didn't even understand that there are so many different types (as SQL variants have some differences but the basic concepts are the same, so I was thinking along the same line).Carmen
What is confusing for me, though, is the part of user - do I save my user in chatSchema? is the String field there functioning like a ForeignKey basically? How does mongo know how to fetch the relevant user information?Carmen
@Carmen - MongoDB has two types of relationships: embed or reference. This post gives you a good outlook on the differences: #5373698, if you use mongoosejs then look at Populate: mongoosejs.com/docs/populate.htmlReceivership
@Carmen - the links on this post are a good starting point on what NoSQL is and how to choose the right NoSQL database for your needs: #5689591Receivership
W
7

NoSQL is not a single standard. To quote something out of MongoDB's website

NoSQL databases typically fall into one of four categories:

  • Key-value stores
  • Wide-column stores
  • Document databases
  • Graph databases

I'm a big fan of Firebase, both the realtime database and Cloud Firestore and I would strongly suggest you to look into it if you're already confident that you want to build this with a non-relational database.

For Cloud Firestore you can follow mostly the same advices that you will get when building for MongoDB. It's a schema-less database with typed fields. Very easy to work with.

There are plenty of material to find on building chats with Firebase real time database.

And you might also find a bunch of posts about building a chat with Firebase in here also:

Wardell answered 12/12, 2018 at 8:26 Comment(2)
Thanks for the helpful links. I didn't award the bounty as this is a good amount of sources but isn't actually an answer to my question. Still, you got an upvote and my gratefulness :)Carmen
re: your friendlychat-web link, github.com/firebase/codelab-friendlychat-web/blob/master/… has some good code to steal for building a firebase db. Lines 53 to 63 for exampleNewspaperwoman
R
4

As others have pointed out NoSQL is a generic term that refers to any alternative to traditional relational databases in which data is placed in tables and data schema is carefully designed before the database is built.

You mentioned Mongo in your question... MongoDB is schema-less. What you can do is create your own class that interacts with an instance of a Mongo Database and in that class you define rules that the data needs to adhere to.

If you are using node.js, you can install Mongoose which allows you to interact with database in object oriented style by providing a straight-forward, schema-based solution to model your data.

Here is a very simple example on how you would define a chat schema in Mongoose, it is not meant to be a complete schema, it is just a start which hopefully will get you going in implementing what you need:

var chatSchema = new Schema({
    chatSession: { type: Number, index: true },    
    user: { type: String, default: 'anonymous' },
    chatLineText: { type: String },
    dateTime: { type: Date, default: Date.now },
});

var chatModel = mongoose.model('Chat', chatSchema);
var chatLine1 = new chatModel({
    chatSession: '2133123',
    user: 'someUserName',
    chatLineText: 'Hello yuvi!'
});

chatLine1.save(function (err, chatLine) {
    if (err) console.log(err);
    else console.log('following chatLine was saved:', chatLine);
Receivership answered 11/12, 2018 at 15:44 Comment(5)
Here is a tutorial on how to make a chat application using MongoDB + Mongoose: dzone.com/articles/…Receivership
Thank you for the detailed explanation. As I said, NoSQL is new to me so I didn't even understand that there are so many different types (as SQL variants have some differences but the basic concepts are the same, so I was thinking along the same line).Carmen
What is confusing for me, though, is the part of user - do I save my user in chatSchema? is the String field there functioning like a ForeignKey basically? How does mongo know how to fetch the relevant user information?Carmen
@Carmen - MongoDB has two types of relationships: embed or reference. This post gives you a good outlook on the differences: #5373698, if you use mongoosejs then look at Populate: mongoosejs.com/docs/populate.htmlReceivership
@Carmen - the links on this post are a good starting point on what NoSQL is and how to choose the right NoSQL database for your needs: #5689591Receivership

© 2022 - 2024 — McMap. All rights reserved.