find方法在mongoDB中不过滤结果



我有一个方法,我从数据库中检索数据,像这样。

const popularBookGenreDB = async (genre) => {
try {
//Remaining to filters books by genre available
console.log("{ genres: genre }", { "genres": genre });
const enw = await Books.find({ genres: genre }).sort({ bbeScore: -1 }).limit(2)
return enw;
} catch (error) {
throw error;
}};

它应该通过查看特定类型是否在元素的类型列表中来过滤结果,但它只返回按bbeScore排名的项目。它根本没有考虑到类型列表。

当我在MongoDB Atlas上运行相同的查询{genres : "Romance"}时,它按预期工作。但是当我尝试在节点服务器上运行它时,它没有考虑到流派列表。

我有文档为。

{
"_id": "62b77562076177189c8f53af",
"title": "The Hunger Games",
"series": "The Hunger Games #1",
"author": [
"Suzanne Collins"
],
"rating": 4.33,
"description": "WINNING MEANS FAME AND FORTUNE.LOSING MEANS CERTAIN DEATH.THE HUNGER GAMES HAVE BEGUN. . . .In the ruins of a place once known as North America lies the nation of Panem, a shining Capitol surrounded by twelve outlying districts. The Capitol is harsh and cruel and keeps the districts in line by forcing them all to send one boy and once girl between the ages of twelve and eighteen to participate in the annual Hunger Games, a fight to the death on live TV.Sixteen-year-old Katniss Everdeen regards it as a death sentence when she steps forward to take her sister's place in the Games. But Katniss has been close to dead before—and survival, for her, is second nature. Without really meaning to, she becomes a contender. But if she is to win, she will have to start making choices that weight survival against humanity and life against love.",
"language": "English",
"isbn": "9780439023481",
"genres": [
"Young Adult",
"Fiction",
"Dystopia",
"Fantasy",
"Science Fiction",
"Romance",
"Adventure",
"Teen",
"Post Apocalyptic",
"Action"
],
"characters": [
"Katniss Everdeen",
"Peeta Mellark",
"Cato (Hunger Games)",
"Primrose Everdeen",
"Gale Hawthorne",
"Effie Trinket",
"Haymitch Abernathy",
"Cinna",
"President Coriolanus Snow",
"Rue",
"Flavius",
"Lavinia (Hunger Games)",
"Marvel",
"Glimmer",
"Clove",
"Foxface",
"Thresh",
"Greasy Sae",
"Madge Undersee",
"Caesar Flickerman",
"Claudius Templesmith",
"Octavia (Hunger Games)",
"Portia (hunger Games)"
],
"bookFormat": "Hardcover",
"edition": "First Edition",
"pages": 374,
"publisher": "Scholastic Press",
"publishDate": "09/14/08",
"firstPublishDate": "",
"awards": [
"Locus Award Nominee for Best Young Adult Book (2009)",
"Georgia Peach Book Award (2009)",
"Buxtehuder Bulle (2009)",
"Golden Duck Award for Young Adult (Hal Clement Award) (2009)",
"Grand Prix de l'Imaginaire Nominee for Roman jeunesse étranger (2010)",
"Books I Loved Best Yearly (BILBY) Awards for Older Readers (2012)",
"West Australian Young Readers' Book Award (WAYRBA) for Older Readers (2010)",
"Red House Children's Book Award for Older Readers & Overall (2010)",
"South Carolina Book Award for Junior and Young Adult Book (2011)",
"Charlotte Award (2010)",
"Colorado Blue Spruce Young Adult Book Award (2010)",
"Teen Buckeye Book Award (2009)",
"Pennsylvania Young Readers' Choice Award for Young Adults (2010)",
"Rhode Island Teen Book Award (2010)",
"Dorothy Canfield Fisher Children's Book Award (2010)",
"Evergreen Teen Book Award (2011)",
"Soaring Eagle Book Award (2009)",
"Milwaukee County Teen Book Award Nominee (2010)",
"Sakura Medal for Middle School Book (2010)",
"Michigan Library Association Thumbs Up! Award (2009)",
"Florida Teens Read (2009)",
"Deutscher Jugendliteraturpreis for Preis der Jugendjury (2010)",
"Iowa High School Book Award (2011)",
"New Mexico Land of Enchantment Award for Young Adult (2011)",
"Eliot Rosewater Indiana High School Book Award (2010)",
"The Inky Awards for Silver Inky (2009)",
"California Young Readers Medal for Young Adult (2011)",
"Lincoln Award (2011)",
"Kinderboekwinkelprijs (2010)",
"Missouri Truman Readers Award (2011)",
"CYBILS Award for Young Adult Fantasy & Science Fiction (2008)",
"Literaturpreis der Jury der jungen Leser for Jugendbuch (2010)",
"The Inky Awards Shortlist for Silver Inky (2009)",
"Prix Et-lisez-moi (2011)",
"Missouri Gateway Readers Award (2011)",
"Oklahoma Sequoyah Award for High School and Intermediate (2011)",
"Premio El Templo de las Mil Puertas for Mejor novela extranjera perteneciente a saga (2009)",
"Rebecca Caudill Young Readers' Book Award (2011)",
"LovelyBooks Leserpreis for Fantasy (2009)",
"LovelyBooks Leserpreis for Bestes Cover/Umschlag (2009)",
"Premi Protagonista Jove for Categoria 13-14 anys (2010)"
],
"numRating": 6376780,
"ratingByStars": [
"3444695",
"1921313",
"745221",
"171994",
"93557"
],
"likedPercent": 0,
"setting": [
"District 1",
"anem",
"Capito",
"anem",
"Panem (United States)"
],
"coverImg": "https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1586722975l/2767052.jpg",
"bbeScore": 2993816,
"bbeVotes": 30516,
"price": 5.09
},

期望的结果,我将得到的文档,将包含类型项目在(例如。"罗曼史")文件,没有其他。

的例子:预期:

[
{title : "Hunger games" , ... , genres : [... , "Romance"...]}
{title : "Pride and Prejudice" , ... , genres : [... , "Romance"...]}
]

返回输出

[
{title : "Hunger games" , ... , genres : [... , "Romance"...]}
{title : "HarryPotter" , ... , genres : [......]} // No "Romance" in genres list
]

Routes.js文件
// router.route("/books").get(searchBooks)
router.route("/books/popular/:genre").get(PopularBookGenre)
// router.route("/books/popular").get(popularBooks)
router.route("books/autocomplete").get(autoCompleteBooks)

如果这不起作用,我如何使用aggregatepipipeline来解决这个问题。再次查询工作在mongoDB在线查找搜索字段的集合,但不与节点。

使用$all运算符查找数组中包含特定值

的文档
const popularBookGenreDB = async (genre) => {
try {
const enw = await Books.find({'genres': {'$all':[genre]} })
.sort({ bbeScore: -1 })
.limit(2);
return enw;
} catch (error) {
throw error;
}};

最新更新