0

I would like to compare two arrays in find() method. I did do that but it doesn seem to work :

var userOne = group.users[0];
var userTwo = group.users[1];

const selector = {
    $and: [{
        $or: [{
            'users[0]': userOne
        }, {
            'users[0]': userTwo
        }]
    }, {
        $or: [{
            'users[1]': userOne
        }, {
            'users[1]': userTwo
        }]
    }, {
        type: 0
    }]
};
var exists = Groups.find(selector).fetch();
console.log(exists);

When userOne and userTwo exist, it doesnt return anything, but when one of them is undefined it returns all the collection.

Someone can help me please ?

Note: Ths schema of the collection is :

GroupsSchema = new SimpleSchema({
    name: {
        type: String,
        optional: true
    },
    description: {
        type: String,
        optional:true
    },
    type: {
        type: Number
    },
    users: {
        type: [String],
        optional: true
    } });
julio campos
  • 47
  • 1
  • 6

1 Answers1

1

No need to use the $or operators here. You could try something like this (See this SO question for reference):

const selector = {
    $and: [{
            'users': userOne
            },
            {
            'users': userTwo
            },
            {
              type: 0
            }]
    };

Also, check the values in your database to make sure they are correct.

Community
  • 1
  • 1
andresk
  • 2,845
  • 1
  • 13
  • 17