0

Basically im developing a chat application and this mongoose schema represents a Schema for private chat between 2 users.

What im trying to do is to check if id1 and id2 given from the client are in my collection of private chats db. The problem is that this mongoDB query does not work correctly and if these user ids already exists in the Chat documents. a new Chat Document is created with the same _id in user1 and user2 just this time reversed. Thats why i need the $or operator but my query does not work as i intend

const chatSchema = new Schema({

        user1: {

            _id: String,
            username: String,
            fullName: String
        },
        user2: {

            _id: String,
            username: String,
            fullName: String
        },
        messages: [
            {
                _id: false,
                author: String,
                text: String,
                date: Number
            }
        ]

    });

So this is my implementation to check if this relationship between 2 users already exists. If it exists do something if it does not create the document and save it. data.user._id represents the id of the user and data.friend._id represents the id of the user to which you are chatting.

Chat.count(
        {
            $or: [
                {
                    user1: {
                        _id: data.user._id
                    },
                    user2: {
                        _id: data.friend._id
                    }
                },
                {
                    user2: {
                        _id: data.user._id
                    },
                    user1: {
                        _id: data.friend._id
                    }
                }
            ]
        }, (err, count) => {
            if (!count) {
                new Chat({
                    user1: data.user,
                    user2: data.friend,
                    messages: []
                }).save((err, data) => {
                    if (err) {
                        console.log(err);
                    }
                    socket.join(data._id);
                    socket.emit('chats', data);
                });
            }
        });

1 Answer 1

2

You need to use MongoDB dot notation for querying within the $or operator.

Try this in your $or

    {
        $or: [
            {
                "user1._id": data.user._id,                    
                "user2._id": data.friend._id                    
            },
            {
                "user2._id": data.user._id,                     
                "user1._id": data.friend._id     
            }
        ]
    }

As per docs:-

To specify or access a field of an embedded document with dot notation, concatenate the embedded document name with the dot (.) and the field name, and enclose in quotes:

Sign up to request clarification or add additional context in comments.

1 Comment

In addition, I recommend you use a findOne instead of a Count with the same $or condition. Same if statement applies (either error or a returned doc or neither if one doesn't exist) but now you can return the existing instance doc in the socket so that new data could be appended to the messages array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.