*我正在使用 mongodb在nestJs 中撰寫代碼。場景是我有2 個模塊用于流派和書籍。在 mongodb 中, Genres 和 Books 都存盤不同的模式。我有一個@Get 方法,它接受一個流派的 id 并回傳存盤在資料庫中的所有書籍。但我想要的是,該函式將采用一種流派的 id,并應回傳有關該流派的書籍。例如,如果我給出英語流派的 id,那么該函式應該只回傳英語書籍。并不是資料庫中存在的所有書籍。請閱讀所有陳述句和代碼。 而且這個問題的標題可能不對,如果你愿意,你可以提出建議。
我希望你能理解我想問什么。如果您需要有關代碼或其他任何內容的更多詳細資訊,我會提供。
現在我正在嘗試的邏輯是遵循代碼
bookschema.ts. 書籍的架構。這里的“genres_id”是特定流派 id 的 id,一個人在 Posting(@Post) 一本書以及其他書籍屬性時會給出它。
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
@Schema()
export class Book {
@Prop()
name: string
@Prop()
author: string
@Prop()
price: number
@Prop()
genres_id: string
}
export const bookschema = SchemaFactory.createForClass(Book)
export type bookdocument = Book & Document
library.service.ts(library.service for 'genres'),在這個檔案中我試圖實作,getallBooksofSingleGenere(id: any),獲取特定型別書籍的函式,我想寫一個邏輯像“查找({id:genre_id})”。但是我的這個邏輯是不對的。我想要這個函式中的一個邏輯,它將回傳特定型別的書籍。
2-而且我在運行時出錯,而不是在編譯時出錯,說 “this.book_model.find({})” 不是一個函式。我不知道為什么會出現錯誤。
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { BooksService } from './books/books.service';
import { Department, departmentdocument } from './departments-schema';
@Injectable()
export class LibraryService {
constructor(@InjectModel(Department.name) private readonly dep_model: Model<departmentdocument>,private readonly book_model: BooksService) { }
async getalldepartments() {
return this.dep_model.find();
}
async getdepartmentbyid(depID){
return this.dep_model.findById(depID)
}
async getallBooksofSingleGenere(id:any)
{
//return this.book_model.find(id)
//return this.book_model.find({books:id})
//return this.book_model.getallbooks() //this logic returns all the books present in database
return this.book_model.find({});//I want to run this logic somehow.
}
async adddepartment(department) {
const newdepartment = new this.dep_model(department)
return newdepartment.save()
}
async putupdatedepartment(id: string, department: departmentdocument) {
return await this.dep_model.findByIdAndUpdate(id, department)
}
async patchupdatedepartment(id: string, department: departmentdocument) {
return await this.dep_model.findByIdAndUpdate(id, department)
}
async deletedepartment(id: string) {
return await this.dep_model.findByIdAndDelete(id)
}
}
library.controller.ts(流派控制器)。這里的第二個@Get,獲取一個流派 id 并獲取該 id 流派的檔案。第三個@Get,檢查 id 是否屬于流派。第三個@Get,檢查它確保id 必須是一個流派的id。
import { Body, Controller, Delete, Get, HttpException, Param, Patch, Post, Put } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { departmentdocument } from './departments-schema';
import { LibraryService } from './library.service';
@ApiTags('Genre')
@Controller('Genres')
export class LibraryController {
constructor(private lib_service: LibraryService) { }
@Get()
async getalldepartments() {
return this.lib_service.getalldepartments()
}
@Get('genres/:depid')
async getdepartmentbyid(@Param('depid') depid: string) {
return this.lib_service.getdepartmentbyid(depid)
}
@Get('booksofperticulargenre/:id')
async getallBooksofSingleGenere(@Param('id') id: string) {
var check = await this.lib_service.getdepartmentbyid(id);
if (check != null) { return this.lib_service.getallBooksofSingleGenere(id) }
else {
throw new HttpException('ID does not match to any Genres,Please Enter a valid Genre ID', 404)
}
}
@Post()
async adddepartments(
@Body() department: string,) {
const adddepartment = await this.lib_service.adddepartment(department)
return adddepartment
}
@Put(':id')
async putupdatedepartment(@Param('id') id: string, @Body() department: departmentdocument) {
const updatedepartments = await this.lib_service.putupdatedepartment(id, department)
return updatedepartments
}
@Patch(':id')
async patchupdatedepartment(@Param('id') id: string, @Body() department: departmentdocument) {
return await this.lib_service.patchupdatedepartment(id, department)
}
@Delete(':id')
async deletedepartment(@Param('id') id: string) {
await this.lib_service.deletedepartment(id);
}
}
WC-LIBRARY(DATABASE) 有兩個進一步的模式

uj5u.com熱心網友回復:
這些資料庫查詢呼叫(查找、更新..)應該在貓鼬模型上觸發。雖然名稱是 book_model,但我不認為它實際上是一個書籍模型,因為它的型別是 BooksService。Book 模型應該與注入 dept 模型的方式相同。
流派_id 是自動生成的識別符號 - 或者 - 它指向某個其他集合 (ObjectId) 嗎?
uj5u.com熱心網友回復:
為了獲得所需的場景解決方案,我進行了三處更改。
1.library.sevice.ts中的Constructor如果要使用find()等mongodb函式,應該是這樣的。
@Injectable()
export class LibraryService {
constructor(@InjectModel(Department.name)
private readonly dep_model: Model<departmentdocument>,
@InjectModel(Book.name)private readonly book_model: Model<bookdocument>) { }
2.您還必須添加我之前缺少的以下代碼。就像每當您需要在服務中使用其他模塊(用于資料庫目的的整個模塊)時,您都必須在第二個中引入該模塊和模塊的架構模塊的匯入(如果我只有 2 個模塊,所以在你的情況下你必須弄清楚你想在其他模塊中匯入哪個模塊)否則你會得到一個錯誤,比如'確保索引處的模塊應該在 x_module 中'
@Module({
imports: [BooksModule,
MongooseModule.forFeature([{
name: Department.name,
schema: departmentschema
}]),
MongooseModule.forFeature([{
name: Book.name,
schema: bookschema
}]),
],
3.獲取特定型別書籍的功能將是這樣。如果出現某種錯誤,您可以使用字串型別更改id。要了解資料庫查詢的作業原理,您可以訪問該鏈接(我建議簡要而徹底地閱讀檔案)https://docs.mongodb.com/manual/reference/method/db.collection.find/。
async getallBooksofSingleGenere(id:any)
{
return this.book_model.find({genres_id:id})
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/350151.html
標籤:MongoDB 猫鼬 mongodb-查询 嵌套
