猫鼬查询从结果中排除一个值 [英] Mongoose query to exclude a value from the result

查看:73
本文介绍了猫鼬查询从结果中排除一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检索一个用户列表,但不包括当前用户ID.

I want to retrieve a users list but excluding the current user ID.

现在我没有排除它了

User.find()
  .or([
     {email: new RegExp(req.param('searchTerm'), 'i')}, 
     {displayName: new RegExp(req.param('searchTerm'), 'i')}
   ],
'displayName email profileImageURL')
  .limit(20)
  .exec(function(err, users)

如何更改查询以使用AND,并且我需要排除当前的用户ID?

How can I change the query to use the AND I need to exclude the current userID?

谢谢

推荐答案

为此在查询链中添加初始where调用:

Add an initial where call to your query chain for that:

User.find()
  .where('_id').ne(userID)
  .or([
     {email: new RegExp(req.param('searchTerm'), 'i')}, 
     {displayName: new RegExp(req.param('searchTerm'), 'i')}
   ])
  .select('displayName email profileImageURL')
  .limit(20)
  .exec(function(err, users)

or调用使它看起来像是两个OR(或)在一起,但实际上是AND(与)在一起.

The or call makes it read like the two are OR'ed together but they're actually AND'ed.

这篇关于猫鼬查询从结果中排除一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆