简体   繁体   English

使用 mongoose 查找与数组之一匹配的文档

[英]Using mongoose to find a document that matches one of array

I have a document with the bankAccounts attribute:我有一个带有bankAccounts属性的文档:

const UsersSchema = new mongoose.Schema({
    user_id: {
        type: String,
        required: true,
    },
    bankAccounts: [
        {
            currency: String,
            personType: String,
            bankName: String,
            branchCode: String,
            accountNumber: String,
            bic: String,
            bankCity: String,
            accountType: String,
            isPrimary: Boolean,
        },
    ]
})

So a user can have many bank accounts.所以一个用户可以有很多银行账户。 It can have currency: USD and a currency: EUR bank accounts.它可以有currency: USDcurrency: EUR银行账户。

I'm querying the users and retrieving one user with an array of currencies this user have, so in this example my array would be ['USD', 'EUR'] .我正在查询用户并使用该用户拥有的一系列货币检索一个用户,因此在此示例中,我的数组将是['USD', 'EUR']

I need to make a new query, to a new collection, searching all orders that match one of the currencies in the array:我需要对新集合进行新查询,搜索与数组中一种货币匹配的所有订单:

const orders = await Orders.find({status: "Allocating", currency: oneOfTheArray })

How can I achieve an OR condition on my query, iterating all currencies in my array?如何在查询中实现OR条件,迭代数组中的所有货币?

Thank you in advance先感谢您

Try this尝试这个

const orders = await Orders.find({status: "Allocating", currency: {$in:oneOfTheArray} })

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

相关问题 Mongoose 查找数组包含至少一个与 id 匹配的对象的位置 - Mongoose find where array contains at least one object that matches with an id 如何使用 mongoose 将数组中的每个项目保存为文档? 如果任何文档与数组中的项目匹配,则不应创建它 - How to save each item in an array as a document using mongoose ? If any document matches item in array, it should not be created MongoDb:find(),文档的属性匹配给定数组的一个值 - MongoDb: find(), property of document matches one value of given array 给定一个 mongodb 模型的数组,我想使用 mongoose 找到该数组最匹配的记录 - Given a mongodb model's array I want to find records in which that array has the most matches using mongoose 猫鼬查找-排除一份特定文档 - Mongoose Find - Exclude One Specific Document ZCCADCDEDB567ABAE643E15DCF0974E503Z:通过数组字段中的值查找文档 - Mongoose: find document by values inside an array field 如果Mongoose数组中包含元素,则查找Mongodb文档 - Find a Mongodb document if it contains an element in the array in mongoose Mongoose 从文档数组中查找和删除对象 - Mongoose FInd and remove object from array of a document Mongoose找到多个匹配 - Mongoose find with multiple matches ZCCADCDEDB567ABAE643E15DCF0974E503Z 计算一个文档内的字符串数组 - Mongoose to count string array inside one document
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM