1

I have to know if an item is in the first or in the second arrays in this model.

Model:
_id: String,
array1: [{ id: Number, name: String, timestamp: String }],
array2: [{ id: Number, name: String, timestamp: String }],
array3: [{ id: Number, name: String, timestamp: String }],
array4: [{ id: Number, name: String, timestamp: String }]

});

I am using these queries but can anyone help me to improve it or maybe to do it in only one query?

Model.findOne({_id: userId , 'array1.id': item.id },function(error, res) {
    if(!res){
      Model.findOne({_id: userId , 'array2.id': item.id },function(error, res) {
        if(!res){
          //do what i need;
          }else{
            //error;
          }
        });
    }else{
      //error;
    }
  });

I have already used queries like :

Model.findOne({$and: [{_id: userId} ,{'array1.id': item.id }, {'array2.id': item.id} ]},function(error, res) {
    if(!res){

But it doesn't work.

1 Answer 1

1

You can use the $or statement:

Model.findOne({$and: [{_id: userId} , { $or: [ {'array1.id': item.id }, {'array2.id': item.id} ] } ]},function(error, res) { if(!res){....
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you... i have mistaken each other ( and and or ).

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.