Home > Enterprise >  findOne is gaving me the same result for different IDs
findOne is gaving me the same result for different IDs

Time:09-22

I have a list of different Ids , and I'm trying to find the products that have those Ids and push them in another array. The problem is that when I use findOne() return the same product for all Ids.

let  wishpro = ['632a5e5rtybdaafd23','65fdaartbdqip55'];
            
            for(let i = 0; i < wishpro.length ; i  ) {
                let x = await product.findOne({id : wishpro[i]});
                console.log("x : "   x.id )
                wishProducts.push(x);
            }

CodePudding user response:

findOne() will return the first match that it finds. Instead, you can use find() with $in operator, and fetch all of the records with one query:

const wishpro = ['632a5e5rtybdaafd23','65fdaartbdqip55'];

const result = await product.find({ id: { $in: wishpro } });
  • Related