W
W
Wasya UK2017-05-01 18:29:34
MongoDB
Wasya UK, 2017-05-01 18:29:34

What is the correct way to access an array in Mongoose?

There is a message schema that stores all messages from users.
Each user has an array of dialogs (correspondences), each of which stores message ids. The diagram looks like this:

dialogues: [{
        name: mongoose.Schema.Types.ObjectId, // contain order id
        messages: [{
            id: {
                type: mongoose.Schema.Types.ObjectId
            }, // contain message id
            properties: {
                hidden: {
                    type: Boolean,
                    default: false
                },
                new: {
                    type: Boolean,
                    default: true
                }
            }
        }]
    }]

When a message is written to general messages, a function is called for the sender and recipient, which writes to their correspondence, the id of this message. Here is the function:
function insertUserMessages(userId, dialogTitle, message) {
    User.findById(userId, function(err, user) {
        
        User.update(
            {dialogues: { $elemMatch: { name: dialogTitle } } },
            { $push: { "dialogues.messages": message } }, 
            { safe: true, upsert: true }, 
            function(err, result) {
                if (err) console.log(err);
            }
        );
        
    });
}

Q: How to write correctly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Wasya UK, 2017-05-03
@dmc1989

The solution was this: The
user's dialogs are stored in this form

"dialogues" : {
        "58f9e5ff408da70504fede05" : [   // <----    человек по-переписке
            {
                "id" : ObjectId("590880f1c612893834123438") // <----   id сообщения, здесь могут быть дополнительные 
            },                                              // параметры. Например, скрыл ли его пользователь.
            ...
        ],
        "58f9fcb6794ac63bf44bd61c" : [ 
            {
                "id" : ObjectId("5908a3470d59a45a64715bb2")
            }, 
            ...
        ]
}

In the user schema, this is an empty object
. There is a separate collection for messages.
After saving to the collection of messages, the id of this message is written to the sender and recipient in the dialogue for a new or existing correspondence
User.findByIdAndUpdate(
    userId, 
    {$push: {['dialogues.' + message.recipient]: {id: message._id}} }, 
    { safe: true, upsert: true }, 
    function(err, user) {       
        if (err) console.log(err);
    }
);

Seems to be working...

E
emp1re, 2017-05-03
@emp1re

I do not pretend to be correct, but I would make a separate collection for dialogues.

_id : mongoid,
author: userId,
recipient: userId,
message: [{
 author: userId,
 text: "",
 timestamps: true
}]

Each document is a "dialogue" we know who created it and to whom it was sent (can be expanded). All history with the chronology of this dialogue, simple requests. In order to get the user from userId, we look towards $lookup.
Your question is not fully understood, do you want to check your version? I think the result of the work is so clear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question