如何实施“等于”对于MongoDB的接口?

发布于 2025-01-27 15:50:13 字数 420 浏览 3 评论 0原文

export interface IHealthPlanCard {
    _id: string,
    statusId: string;
}

const cards: IHealthPlanCard[] = await healthPlanCardsCollection.find(...)
cards.filter(card => card.statusId.equals(cardStatusId))

属性'等于''

在这种情况下,它向我显示了错误: 写这样的东西:

export interface IHealthPlanCard {
    _id: string,
    statusId: string | eqauls;
}
export interface IHealthPlanCard {
    _id: string,
    statusId: string;
}

const cards: IHealthPlanCard[] = await healthPlanCardsCollection.find(...)
cards.filter(card => card.statusId.equals(cardStatusId))

In that case it shows me error: Property 'equals' does not exist on type 'string'.ts(2339)

I can't write something like that:

export interface IHealthPlanCard {
    _id: string,
    statusId: string | eqauls;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-02-03 15:50:13

如果您使用的是mongodb(或mongoose)并处理objectids,则应始终使用ObjectId type而不是String

import { ObjectId } from "mongodb"
// import { Types } from "mongoose"

export interface IHealthPlanCard {
  _id: string,
  statusId: ObjectId;
}

const cards: IHealthPlanCard[] = await healthPlanCardsCollection.find(...)
cards.filter(card => card.statusId.equals(cardStatusId))

这将唯一如果将模式定义为使用ObjectID而不是String

现在,您的objectIds将具有.equals()方法。

If you are using MongoDB (or mongoose) and dealing with ObjectIds, you should always use the ObjectId type instead of string:

import { ObjectId } from "mongodb"
// import { Types } from "mongoose"

export interface IHealthPlanCard {
  _id: string,
  statusId: ObjectId;
}

const cards: IHealthPlanCard[] = await healthPlanCardsCollection.find(...)
cards.filter(card => card.statusId.equals(cardStatusId))

This will only work if you defined your schema to use ObjectId instead of string.

Now your ObjectIds will have the .equals() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文