【问题标题】:How do I search the mongoose data with with multiple properties?如何搜索具有多个属性的猫鼬数据?
【发布时间】:2021-12-09 14:44:20
【问题描述】:

我按用户(userID)创建了一个团队,现在我想搜索该用户是否存在于团队中。

exports.start = async (message) => {

const check = await group.find({userID1:message.author.id} ||{userID2:message.author.id} || {userID3:message.author.id})
console.log(check);
}

我的数据库是这样的

{
    "_id" : ObjectId("6173107fcf643f15e1fced83"),
    "userID1" : "769819933390667796",
    "userName1" : "Davion",
    "userID2" : "247276609894219777",
    "userName2" : "Davion",
    "userID3" : "898413310872006716",
    "userName3" : "Davion",
    "team" : "3",
    "__v" : 0
}

【问题讨论】:

    标签: javascript node.js mongodb mongoose discord.js


    【解决方案1】:

    $或类似逻辑或运算符

    exports.start = async (message) => {
      const check = await group.find({
        $and: [
          {
            $or: [
              { userID1: message.author.id },
              { userID2: message.author.id },
              { userID3: message.author.id }
            ]
          }
        ]
      })
      console.log(check);
    }
    

    $and 对一个或多个表达式(表达式 1、表达式 2 等)组成的数组执行逻辑与运算,并选择满足所有表达式的文档。

    exports.start = async (message) => {
      const check = await group.find({
        $and: [
          { userID1: message.author.id },
          { userID2: message.author.id },
          { userID3: message.author.id }
        ]
      })
      console.log(check);
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-17
      • 2021-04-14
      • 2013-01-23
      • 2019-07-10
      • 2011-11-14
      • 2016-03-28
      • 2016-09-15
      • 2015-02-24
      • 2017-01-07
      相关资源
      最近更新 更多