且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

查找包含特定值的数组的文档

更新时间:2022-11-01 22:05:50

由于favouriteFoods是一个简单的字符串数组,因此您可以直接查询该字段:

As favouriteFoods is a simple array of strings, you can just query that field directly:

PersonModel.find({ favouriteFoods: "sushi" }, ...);

但是我也建议在您的模式中使字符串数组显式显示:

But I'd also recommend making the string array explicit in your schema:

person = {
    name : String,
    favouriteFoods : [String]
}

相关文档可在此处找到: https://docs.mongodb.com /manual/tutorial/query-arrays/

The relevant documentation can be found here: https://docs.mongodb.com/manual/tutorial/query-arrays/