月關(guān)寶盒
2022-12-09 19:07:18
我正在嘗試根據(jù)文檔 ID 數(shù)組查詢多個(gè)文檔。這是一個(gè)示例數(shù)組:[{_id: '123151djadjw11', quantity: 1}, {_id: 'some4idda123131', quantity: 2}];我希望能夠?qū)⑦@樣的數(shù)組發(fā)送到服務(wù)器,并檢索與數(shù)組中每個(gè)對象中的 ID 屬性匹配的文檔。任何想法將不勝感激!提前致謝!
1 回答

絕地?zé)o雙
TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊
let array1 = [{_id: '123151djadjw11', quantity: 1}, {_id: 'some4idda123131', quantity: 2}];
let array2 = array1.map(a => a._id);
//結(jié)果 array2 =['123151djadjw11', 'some4idda123131'];
db 包含值為 { "_id" : "123151djadjw11", "quantity" : 1.0 } { "_id" : "some4idda123131", "quantity" : 2.0 } { "_id" : "3rdId", "quantity" : 5.0 }
let array3 = db.objects.find({_id:{$in : array2}}).toArray();
//返回 array3= [{_id: '123151djadjw11', 數(shù)量: 1}, {_id: 'some4idda123131', 數(shù)量: 2}]
添加回答
舉報(bào)
0/150
提交
取消