简体   繁体   中英

Get multiple documents from collection using nodejs and mongodb

Hi I have two mongodb collections. The first one returns json data (array) and with the output of this, I want to return documents that match.

When I run Console.log (req.bidder.myBids) I get the following output:

 [{"productId":"3798b537-9c7b-4395-9e41-fd0ba39aa984","price":3010},{"productId":"3798b537-9c7b-4395-9e41-fd0ba39aa984","price":3020},{"productId":"4c4bd71c-6664-4d56-b5d3-6428fe1bed19","price":1040},{"productId":"4c4bd71c-6664-4d56-b5d3-6428fe1bed19","price":1050},{"productId":"4c4bd71c-6664-4d56-b5d3-6428fe1bed19","price":1060},{"productId":"4c4bd71c-6664-4d56-b5d3-6428fe1bed19","price":1070},{"productId":"4c4bd71c-6664-4d56-b5d3-6428fe1bed19","price":1090},{"productId":"4c4bd71c-6664-4d56-b5d3-6428fe1bed19","price":1100}]

The productId has duplicates, I want to remove duplicates and then call a routine that finds all the products that match and output as json. So far I have this code that only outputs one document, but cant figure out how to add the array of productId's and then fetch all corresponding products.

    dbProduct.find({
        agencyId: agencyId, // this is always fixed 
        productId: productId // i need to add values of productId here (currently works for single value only)
    }).then(dbRes => { 
    

console.log(dbRes);

The $in operator is what you want. See the docs here: https://docs.mongodb.com/manual/reference/operator/query/in/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM