如何从猫鼬获取输入的对象

时间:2020-09-17 00:28:14

标签: node.js mongodb typescript mongoose mongoose-schema

我在typescript中有一个nodejs应用程序,最近我开始使用mongoose来查询mongo db,但是我一直在努力寻找一种从findById方法获取类型化对象的方法。是否可以通过某种方法从该方法中获取类型化的对象?

sheetName

1 个答案:

答案 0 :(得分:0)

您可以为该架构创建,导出和接口:

import mongoose, { Schema, Document, Model } from 'mongoose';

export interface ILinksCacheSchema extends Document {
    ts: Date,
    imdbId: string,
    parentLink: string,
    playableLink: string,
    status: string,
    title: string,
    size: number,
    contentType: string,
}

const LinksCacheSchema = new Schema({
    ts: {
        type: Date,
        default: Date.now()
    },
    imdbId: String,
    parentLink: String,
    playableLink: String,
    status: String,
    title: String,
    size: Number,
    contentType: String,
}, { collection: 'links_cache' });

export const LinksCacheList: Model<ILinksCacheSchema> = mongoose.model('LinksCache', LinksCacheSchema);

// in your app logic
const linkInfo = await LinksCacheList.findById(documentId);

两次创建模式可能会变得有些乏味,所以我建议您看一下:

https://www.npmjs.com/package/ts-mongoose