首页 > 解决方案 > mongodb findMany by id 在数组中的所有记录中

问题描述

我需要通过 ids 数组从数组文件中的每个元素中获取数据库对象["c5f2d584-60ab-4068-b567-9b422f6c4e24","09c0ef39-55ea-45b4-88d3-a8f97730d6d3"]

如何提出这样的要求?

在此处输入图像描述

model.find({
    files: '_id' : ["c5f2d584-60ab-4068-b567-9b422f6c4e24","09c0ef39-55ea-45b4-88d3-a8f97730d6d3"]
}).
then(res => {
    console.log(res)
});

标签: node.jsmongodbexpressmongoose

解决方案


您可以像这样使用 $in 运算符:

model.find({
    "files._id": { $in: ["c5f2d584-60ab-4068-b567-9b422f6c4e24","09c0ef39-55ea-45b4-88d3-a8f97730d6d3"] }
}).
then(res => {
    console.log(res)
});

推荐阅读