Home > database >  document not filtered by property
document not filtered by property

Time:12-06

My document item looks like that: enter image description here

I'm trying to filter the document by year like that:

async function getBooksByYear(year: number): Promise<Book[]> {

     return BookModel.find({'year': year})
}

I send the request from postman: http://localhost:4000/api/books?year=1990 enter image description here

but it's not working - it's returning the entire document items

appreciate any help

CodePudding user response:

The answer is: this property wasn't declared in the schema.

when I add the year to the schema:

 export const BookSchema = new mongoose.Schema<Book>({
author: {
    type: String,
    required: [true, "missing author name"]
},
country: {
    type: String,
    required: [true, "missing country"]
},

// I add this part now
year: {
    type: Number
  }
})

it works :)

  • Related