I am trying to combine two queries in Mongoose into one.
Right now, I'm doing this:
User.find({ _id: req.body.userId }, (err, user) => {
User.find({ username: decodedUser.username }, (err, member) => {
console.log("user\n", user)
console.log("member\n", member)
})
})
I'd like to make a single User.find() query to the database, and the result be an object (or an array) of all the documents that I want, such as an array of [user, member] in this case. This will save me from having to run User.find() twice. Ideally, I'd like the result from the query to be an object so that I can find a specific key/value set more easily, as opposed to an array that I would have to iterate through to find a particular element. How do I do this?
Note: this works:
User.find({ $or:
[
{ _id: req.body.userId },
{ username: decodedUser.username}
]}, (err, results) => {
console.log("results\n", results)
})
where results is an array of the documents that I want. But, I'd like to know if there is a better way.