【问题标题】:How to filter in Mongoose?如何在猫鼬中过滤?
【发布时间】:2021-09-29 21:02:55
【问题描述】:

我想对 id 做一个过滤操作。我希望跳过 id 为 3 的数据。

示例

const id=3;

const data =[
{id:1},
{id:2},
{id:3},
{id:4},
{id:5},
];

// the result i want

[{id:1},{id:2},{id:4},{id:5}]

如何在 Mongoose 中做到这一点?

【问题讨论】:

  • 除非设置聚合管道,否则无法从文档内的列表中过滤条目

标签: javascript mongodb mongoose


【解决方案1】:
yourModelName.find({ id: { $ne: 3 } })

$ne:不等于

更多关于查找方法:https://mongoosejs.com/docs/api/query.html#query_Query-find

新运营商:https://docs.mongodb.com/manual/reference/operator/query/ne/

【讨论】:

  • 非常感谢。
【解决方案2】:

Find 是你的朋友。您可以对要搜索的字段应用条件。在这种情况下,它是id$ne 是一个查询条件,意思是不等于给定的值。

您的查询将如下所示:

const number = 3
const result = await users.find({ id : { $ne: number } })

【讨论】:

  • 非常感谢。
猜你喜欢
  • 2019-12-29
  • 1970-01-01
  • 2021-01-18
  • 2020-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-12
  • 2017-01-06
相关资源
最近更新 更多