Georgi Antonov
Georgi Antonov

Reputation: 1641

mongodb mongoose simple nested query with $or

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);
                });
            }
        });

Upvotes: 0

Views: 1469

Answers (1)

Tarush Arora
Tarush Arora

Reputation: 576

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:

Upvotes: 2

Related Questions