Home > OS >  How do I search the mongoose data with with multiple properties?
How do I search the mongoose data with with multiple properties?

Time:10-23

I made a team by users(userID), now i want to search if that user is present in the team or not.

exports.start = async (message) => {

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

My database looks like this

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

CodePudding user response:

$or like logical or operator

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 performs a logical AND operation on an array of one or more expressions (expression1, expression2, and so on) and selects the documents that satisfy all the expressions.

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);
}
  • Related