繁体   English   中英

如何 select 仅具有来自 ids 数组的 id 的产品。 MongoDB

[英]How to select only products with ids from ids array. MongoDB

我正在获取所有产品并使用 mongoDB 填充该类别。此外,我还有带有类别 ID 的数组,我只想获取类别 object 中具有该 ID 的产品。如何仅使用 select 此产品?

 arr of ids [ '615e94cab42d2274f0481232' ] arr of products [ { _id: new ObjectId("615e945cb42d2274f0481211"), image: '', category: { _id: new ObjectId("615da90b689b8ef91fa90afd"), name: 'Разное' }, name: '435пукп', type: 'В упаковке', piecesPerPackage: 2, pricePerPiece: 1, pricePerPackage: 2, weight: 12, description: 'вапвап' }, { _id: new ObjectId("615eaa08f68e788c63817641"), image: '', category: { _id: new ObjectId("615e94cab42d2274f0481232"), name: '2' }, name: '2', type: 'В упаковке', piecesPerPackage: 2, pricePerPiece: 2, pricePerPackage: 2, weight: 2, description: '2' } ]

 async sortCatalog(arr) { const products = await CatalogModel.find().lean().populate("category", "name").select('-__v'); console.log("arr of ids", arr) console.log("arr of products", products) }

使用category._id找到它。

await CatalogModel.find({'category._id':new ObjectId("615e94cab42d2274f0481232")})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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