0

Say I have this userSchema:

const userSchema = new Schema({
    firstName: {type: String, required: true},
    lastName: {type: String, required: true},
    email: {type: String, required: true, unique: true},
});

and this chatSchema:

const chatSchema = new Schema({
    user_ids: {type: [String]},
});

I wonder if I could do a search to get all chats from a user that has their user_id inside chat's user_ids and how could I do that.

ERP
  • 325
  • 2
  • 11
  • What does `user_ids: {type: [User]}` mean ? what is the relation between userSchema & chatSchema ? – whoami - fakeFaceTrueSoul Jun 16 '20 at 18:15
  • I am sorry, it is an array of user userSchema ids I have changed it – ERP Jun 16 '20 at 18:17
  • 1
    Do you mean `_id`'s of `userSchema` ? You can use `$lookup` to join to collections, check this :: https://stackoverflow.com/questions/2350495/how-do-i-perform-the-sql-join-equivalent-in-mongodb , that exactly what you're looking for !! – whoami - fakeFaceTrueSoul Jun 16 '20 at 18:18
  • Thanks for the response! Maybe I am approaching this in the wrong way but I am quite new in Mongo I am thinking the best way to do this. What I am trying to do is create a chat that may have many users in it and store all those user's ids inside user_ids in the chatSchema, and later I want to be able to find all chats where a certain user belongs, am I approaching this right? – ERP Jun 16 '20 at 18:28
  • 1
    Data design will be pretty complex & can be dealt by you & your team by looking at your application needs. But yes you can refer `_id` of userSchema in chatSchema or you can maintain another field `user_id` in userSchema and that can be referred in chatSchema - So there are many ways to do it all depends on your need. In another way you can refer `chatId` in chats array in userSchema if you wanted to maintain very few chats !! Check this :: (https://docs.mongodb.com/manual/core/data-modeling-introduction/) – whoami - fakeFaceTrueSoul Jun 16 '20 at 18:38

1 Answers1

1

If you are referencing the user schema and you just need to query the user_id you can simply do it like this:

 db.chat.aggregate([{$macth:{"user_ids" : user_id}}])
Ana Lava
  • 769
  • 1
  • 6
  • 13
  • What I am trying to do is create a chat that may have many users in it and store all those user's ids inside user_ids in the chatSchema, and later I want to be able to find all chats where a certain user belongs. – ERP Jun 16 '20 at 18:33
  • @Ana Lava : This will work, if `_id` of user schema is handy, which in most cases isn't the scenario & maybe there is field called `user_id` in userSchema & referred in chatSchema !! Additionally it needs two DB calls to get this work done - works well if app is designed for multiple Ajax calls but we can avoid 2nd BD call for n number of user's for n num of times if we can wait for few ms to get chats in one go.. – whoami - fakeFaceTrueSoul Jun 16 '20 at 18:34
  • Yeah, you can do it like this with this approach. – Ana Lava Jun 16 '20 at 18:34
  • @whoami If the user_id is not available and another field needs to be queried to get the chats of a user, it can be done with `$lookup` – Ana Lava Jun 16 '20 at 18:38
  • @AnaLava : Didn't get that ! – whoami - fakeFaceTrueSoul Jun 16 '20 at 18:40
  • So maybe what I need to do is create another let's call it usersToChatSchema where for every user I store a chat_id where this user belongs? – ERP Jun 16 '20 at 18:42
  • 1
    @PRE :No, There is no need to do that, you can query with this design. Just as @whoami mentioned, it's better to define which field you are referencing in your schema. Like this: `user_ids: [{type: String, ref: users}]` – Ana Lava Jun 16 '20 at 18:44
  • Great! Thank you guys! – ERP Jun 16 '20 at 18:45