Mongoose find 查询返回json数据处理方式(mongo查找)硬核推荐

随心笔谈11个月前发布 admin
100 0


async findById(id: string | string[]) {
let res
try {
if (Array.isArray(id)) {
res=await this.dataModel.find({ _id: { $in: id } })
} else {
res=await this.dataModel.findById(id)
}
} catch (error) {
throw new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR)
}
return res;
}

async copyDataModal(dataModel: CopyDataModelDto) {
let res
try {
const { id }=dataModel
const modalData=await this.findById(id)
if (modalData) {
modalData.props=(modalData.props || []).map((ele: any)=> {
return dataMasking(ele, [‘_id’])
})
const addData=dataMasking({ …modalData, …dataModel }, [‘_id’, ‘id’, ‘__v’])
// res=await this.add(addData)
res=addData
}

} catch (error) {
throw new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR)
}
return res
}

© 版权声明

相关文章