按类别获取所有记录 NestJs + MongoDB + Mongoose
Get all records by category NestJs + MongoDB + Mongoose
我正在使用 NestJs + MongoDB + Mongoose,我想用我通过参数发送的记录获取 MongoDB 中的所有记录,但我没有得到它,我是初学者。我怎样才能获得同一类别的所有记录?
我在请求中发送了类别 ID,但我没有收到该类别的所有记录,你能帮我吗?
我需要这个:
获取/users/food
和 return 这个:
{
"password": "123",
"name": "Brian",
"adress": "",
"email": "a@a",
"category": "food",
"cpfOrCnpj": "string"
},
{
"password": "123",
"name": "Margo",
"adress": "",
"email": "a@a",
"category": "food",
"cpfOrCnpj": "string"
}
我的代码:
我的服务:
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { User } from './user.model';
import { Model } from 'mongoose';
@Injectable()
export class UserService {
constructor(@InjectModel('User') private readonly userModel: Model<User>) {}
async create(doc: User) {
//Ok
const result = await new this.userModel(doc).save();
return result.id;
}
async find(id: string) {
return await this.userModel.findById(id).exec();
}
async update(user: User) {
//Test
return await this.userModel.findByIdAndUpdate(user);
}
}
我的控制器:
import { Body, Controller, Get, Param, Post, Put } from '@nestjs/common';
import { UserService } from './user.service';
import { User } from './user.model';
@Controller('user')
export class UserController {
constructor(private service: UserService) {}
@Get(':id')
async find(@Param('category') id: string) {
return this.service.find(id);
}
@Post('create')
create(@Body() user: User) {
return this.service.create(user);
}
@Put('update')
update(@Body() user: User) {
return this.service.update(user);
}
}
在这个函数中
find(id: string) {
return this.userModel.findById(id).exec();
}
您正在按 _id
搜索,findById
方法用于按文档 _id
过滤
我认为 category
不是你这里文档的 _id
所以,你需要使用正常的find
方法,并传递一个对象给它
find(id: string) { // id is not _id here, I suggest you to name it category instead
return this.userModel.find({ category: id }).exec();
}
Note, you don't need the async/await here, as you are returning the promise itself
希望对您有所帮助
我正在使用 NestJs + MongoDB + Mongoose,我想用我通过参数发送的记录获取 MongoDB 中的所有记录,但我没有得到它,我是初学者。我怎样才能获得同一类别的所有记录? 我在请求中发送了类别 ID,但我没有收到该类别的所有记录,你能帮我吗?
我需要这个:
获取/users/food 和 return 这个:
{ "password": "123", "name": "Brian", "adress": "", "email": "a@a", "category": "food", "cpfOrCnpj": "string" },
{ "password": "123", "name": "Margo", "adress": "", "email": "a@a", "category": "food", "cpfOrCnpj": "string" }
我的代码:
我的服务:
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { User } from './user.model';
import { Model } from 'mongoose';
@Injectable()
export class UserService {
constructor(@InjectModel('User') private readonly userModel: Model<User>) {}
async create(doc: User) {
//Ok
const result = await new this.userModel(doc).save();
return result.id;
}
async find(id: string) {
return await this.userModel.findById(id).exec();
}
async update(user: User) {
//Test
return await this.userModel.findByIdAndUpdate(user);
}
}
我的控制器:
import { Body, Controller, Get, Param, Post, Put } from '@nestjs/common';
import { UserService } from './user.service';
import { User } from './user.model';
@Controller('user')
export class UserController {
constructor(private service: UserService) {}
@Get(':id')
async find(@Param('category') id: string) {
return this.service.find(id);
}
@Post('create')
create(@Body() user: User) {
return this.service.create(user);
}
@Put('update')
update(@Body() user: User) {
return this.service.update(user);
}
}
在这个函数中
find(id: string) {
return this.userModel.findById(id).exec();
}
您正在按 _id
搜索,findById
方法用于按文档 _id
过滤
我认为 category
不是你这里文档的 _id
所以,你需要使用正常的find
方法,并传递一个对象给它
find(id: string) { // id is not _id here, I suggest you to name it category instead
return this.userModel.find({ category: id }).exec();
}
Note, you don't need the async/await here, as you are returning the promise itself
希望对您有所帮助