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.

yuvi
  • 18,155
  • 8
  • 56
  • 93
  • 2
    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. – ctrl-alt-delor Dec 11 '18 at 13:00
  • Depends what you want to model. – ctrl-alt-delor Dec 11 '18 at 13:02
  • 2
    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 answer – Sven Hakvoort Dec 11 '18 at 14:34

2 Answers2

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:

Dennis Alund
  • 2,916
  • 1
  • 13
  • 34
  • 1
    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 :) – yuvi Dec 12 '18 at 23:53
  • re: your friendlychat-web link, https://github.com/firebase/codelab-friendlychat-web/blob/master/cloud-functions/public/scripts/main.js has some good code to steal for building a firebase db. Lines 53 to 63 for example – Roly Poly May 14 '20 at 22:08
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);
Leo
  • 5,013
  • 1
  • 28
  • 65
  • 1
    Here is a tutorial on how to make a chat application using MongoDB + Mongoose: https://dzone.com/articles/creating-a-chat-application-in-node-js-with-expres – Leo Dec 12 '18 at 14:27
  • 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). – yuvi Dec 12 '18 at 23:53
  • 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? – yuvi Dec 12 '18 at 23:55
  • 1
    @yuvi - MongoDB has two types of relationships: embed or reference. This post gives you a good outlook on the differences: https://stackoverflow.com/questions/5373198/mongodb-relationships-embed-or-reference, if you use mongoosejs then look at Populate: https://mongoosejs.com/docs/populate.html – Leo Dec 13 '18 at 09:45
  • 1
    @yuvi - 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: https://stackoverflow.com/questions/5689091/how-to-choose-which-type-of-nosql-to-use – Leo Dec 13 '18 at 09:50