Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

node.js - Use or together with and operators in mongoose

I'm trying to filter using multiple items in a nested object in mongoose. The model is like this:

{
    name: String
    ...
    links: [{url: String, canQuery: bool}]
}

I'm trying to make a filter condition to do $or and an and operator like this:

name: req.params.name,
// and either
'links.url': {$ne:req.params.url},
// or
'links.url': req.params.url, 'links.canQuery': false

Not sure the correct way to go about it. I know there's a $or operator, but don't know how to use it together with ands...

question from:https://stackoverflow.com/questions/65851600/use-or-together-with-and-operators-in-mongoose

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
{
  name: req.params.name,
  $or: [ // and either    
    {'links.url': {$ne: req.params.url}},
    // or
    {$and: [
        'links.url': req.params.url, 
        'links.canQuery': false
      ]
    }
  ]
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...