How to use same field multiple times in MongoDB find query in NodeJS

前端 未结 1 1411
無奈伤痛
無奈伤痛 2021-01-23 18:26

I have a MongoDB collection with records having a \"name\" field, I am trying to perform a find query where the name field appears twice in the query. I want to exc

相关标签:
1条回答
  • 2021-01-23 18:58

    Your query JSON object contains name field two times, and it breaks the query. Pay attention to the $and mongo query operator. There are two ways to construct correct query:

    1) db.users.find({ $and: [{ name: { $nin: [current_user] } }, { name: { $regex: new RegExp(/query/) } }] })

    2) db.users.find({ name: { $nin: [current_user], $regex: new RegExp(/query/) } })

    Also, if you exclude only one user, you can use $ne operator instead of $nin.

    0 讨论(0)
提交回复
热议问题