0

How could query multiple value in one collection?

const user = db.collection('users').findOne({ username: 'john' })
const user = db.collection('users').findOne({ email: '[email protected]' })

Below are some dummy data. i hope could query name john and email docs in one line of code.

[{id:1, username: 'john', email: '[email protected]'},{id:2, username: 'kelvin', email: '[email protected]'}, {id:3, username: 'angle', email: '[email protected]'}]

Return Data Are: only id: 1 and 2. becasue they match the query

    [{id:1, username: 'john', email: '[email protected]'},{id:2, username: 'kelvin', email: '[email protected]'}]

1 Answer 1

2

You can use $or operator in this way:

db.collection.find({
  "$or": [
    {
      "username": "john"
    },
    {
      email: "[email protected]"
    }
  ]
})

So you will find a document which matches username or email.

Exmaple here

Sign up to request clarification or add additional context in comments.

1 Comment

Oh! Thanks you! It is what i look for =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.