简体   繁体   English

猫鼬查找-从查询中返回至少匹配一个的所有对象

[英]Mongoose find - return all that match at least one from query

I'm building an application (MongoDB database) where user can register an account. 我正在构建一个应用程序(MongoDB数据库),用户可以在其中注册帐户。 The account model has 3 value's that should be unique: ID, Username & Email. 帐户模型具有3个唯一的值:ID,用户名和电子邮件。 I'm using Mongoose v5.5.5 for querying etc. 我正在使用Mongoose v5.5.5进行查询等。

When someone wants to register a new account, I want to do a query (findOne or find) to see if there is a user with that ID OR username OR mail. 当有人想注册一个新帐户时,我想进行一个查询(findOne或查找),以查看是否存在具有该ID 用户名邮件的用户。 I thought the code below would work. 我认为下面的代码可以工作。

let query = {
  'id': sanitized.id,
  'username': sanitized.username,
  'mail': sanitized.mail
}

AccountModel.find(query, (error, data) => {
  // Handle result
})

So lets say there is a user with as username user1 and as email user1@mail.com . 因此,可以说有一个用户名为user1且电子邮件为user1@mail.com的用户 Now another user wants to register user the username user2 but with the same email as user1. 现在,另一个用户想要注册用户名user2,但使用与user1相同的电子邮件。

How can I perform a query that matches on OR the username OR the mail so that I can tell either of those is already being used by another user? 我怎么能执行已经被另一个用户打开用户名邮箱,这样我可以告诉那些既相匹配的查询?

I prefer using one single query to keep the code clean, but I couldn't find a solution yet. 我更喜欢使用一个查询来保持代码干净,但是我还没有找到解决方案。 Hope someone can help. 希望有人能帮忙。

You can use $or aggregation to pass different queries 您可以使用$或聚合来传递不同的查询

 let query = {
      '$or': [
          {'id': sanitized.id},
          {'username': sanitized.username},
          {'mail': sanitized.mail}
      ]
    }

This will return all documents matching any of the queries. 这将返回与任何查询匹配的所有文档。 In my system what I did is actually two queries in Promise.all, one for userName, one for userEmail, for easier validation of the data (result[0] = userName, result[1] = userEmail) 在我的系统中,实际上我在Promise.all中执行了两个查询,一个用于userName,一个用于userEmail,以便于验证数据(结果[0] = userName,result [1] = userEmail)

Eg 例如

Promise.all([
  AccountModel.find({'username': sanitized.username}).exec(),
  AccountModel.find({'mail': sanitized.mail}).exec(),
]).then(validation => {
  if(validation[0]){// username taken }
  if(validation[1]){// email taken }
})

您仍然可以在使用$or 运算符进行查询时使用Find 方法 ,这是一个示例

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

相关问题 如果至少有一个元素匹配则返回所有数组 - return all array if at least one element match 编写查询并从节点js和mongoose中的数据库返回一个值 - writing a query and return one value from a database in node js and mongoose 查询至少有1个关联的位置,但返回全部 - Query where there is at least 1 association, but return all 如何在 Mongoose 中找到与给定查询值匹配的数组字段的最后一个元素的所有文档? - How can you find all documents that match the last element of an array field with a given query value in Mongoose? 如何从给定的项目数组中找到其数据与至少一个项目的条件匹配的文档 - mongoDB - How to find documents that their data match a condition on at least one item from a given array of items - mongoDB 使用搜索查询返回结果猫鼬v4 - Return results mongoose v4 with find query promise 所有 mongoose 找到返回不完整的值 - promise all mongoose find return incomplete value 使用 mongoose 从数组中返回匹配的文档 - return documents that match from an array using mongoose ZCCADCDEDB567ABAE643E15DCF0974E503Z 通过一次查询在多个字段上“查找” - Mongoose "find" on mutilple fields with one query 猫鼬:find()-匹配两个值并仅返回匹配的文档 - Mongoose: find() - match two values and return only the documents matched
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM