嗨,我有兩個 mongodb 集合。第一個回傳 json 資料(陣列),通過這個輸出,我想回傳匹配的檔案。
當我運行時,Console.log (req.bidder.myBids)我得到以下輸出:
[{"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}]
productId 有重復項,我想洗掉重復項,然后呼叫一個例程來查找所有匹配的產品并輸出為 json。到目前為止,我的代碼只輸出一個檔案,但無法弄清楚如何添加 productId 的陣列,然后獲取所有相應的產品。
var agencyId = req.body.agencyId;
var productId = req.body.productId;
if (!validate.STRING(agencyId)) {
res.apiError(messages.server.invalid_request);
} else {
dbProduct.find({productId:{$in:['3798b537-9c7b-4395-9e41-fd0ba39aa984','4c4bd71c-6664-4d56-b5d3-6428fe1bed19']}
}).then(dbRes => {
console.log(dbRes);
更新了代碼并使用硬連線 productId 并更新了上面的代碼。查看如何獲取陣列資料并轉置替換硬接線的 productId
uj5u.com熱心網友回復:
$in 運算子正是您想要的。請參閱此處的檔案:https : //docs.mongodb.com/manual/reference/operator/query/in/
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/362981.html
上一篇:查找物件陣列
