1

I have a mongoose schema that models a user in a social network. In it i want to save an array of all the chats that a user has, a chat consists of a responent (the user a user is chatting with), and an array that represents the conversation.

var userSchema = mongoose.Schema({
    email        : String,
    chats         : [{ respondent : String, conversation : [{ message: String, author : String}]}],
});

How do i find an item in chats that contains a perticular respondent? In the item found, id like to push a message to the conversation.

1 Answer 1

1

You haven't mention any key for inner chat. Specify like this :

var userSchema = mongoose.Schema({
        email        : String,
        chats         : [
         { respondent : String, 
          innerChat:[{ message: String, author : String}]}
       ],
    });

Query for adding document solution is:

var document={
email:"[email protected]",
chats:[{respondent:"UserName",innerChat:[{message:"hello",author:"authorName"}]}]
}
db.collection.insert(document);

Query for finding document on inner query

db.collection.find({"chats.innerChat.author":"authorName"});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks i forgot to put the key, my issue is that i need to find the innerChat (or conversation as i called it) using the respondent.
you mention that you want to push items in the inner chat.please update you question
well, to be fair i said "Using a respondent, how do i push items to the inner chat" but i see where u are comming from, question updated.
I don't want to look for an author, but rather the item that contains a perticular respondent and push back a json object to the conversation in that item.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.