首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mongoDB中的查找方法不过滤结果

mongoDB中的查找方法不过滤结果
EN

Stack Overflow用户
提问于 2022-06-27 18:19:46
回答 1查看 45关注 0票数 1

我有一种方法,可以从这样的数据库中检索数据。

代码语言:javascript
复制
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排列的项。它根本没有考虑到类型列表。

当我在{genres : "Romance"}地图集查找字段上运行相同的查询MongoDB时,它就像预期的那样工作。但是,当我试图在节点服务器上运行它时,它没有考虑到类型列表。

我有文件。

代码语言:javascript
复制
{
        "_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
    },

预期的结果,我将得到文件,将包含类型项目(例如。(“罗曼斯”)没有其他文件。

例子:

预期:

代码语言:javascript
复制
 [
    {title : "Hunger games" , ... , genres : [... , "Romance"...]}
    {title : "Pride and Prejudice" , ... , genres : [... , "Romance"...]}
  ]

返回输出

代码语言:javascript
复制
 [
    {title : "Hunger games" , ... , genres : [... , "Romance"...]}
    {title : "HarryPotter" , ... , genres : [......]} // No "Romance" in genres list
  ]

Routes.js文件

代码语言:javascript
复制
    // router.route("/books").get(searchBooks)
router.route("/books/popular/:genre").get(PopularBookGenre)
// router.route("/books/popular").get(popularBooks)
router.route("books/autocomplete").get(autoCompleteBooks)

如果这不起作用,我如何使用aggregatePipeline来解决这个问题。同样,该查询工作在mongoDB联机查找收集搜索字段中,但不适用于节点。

EN

回答 1

Stack Overflow用户

发布于 2022-06-27 19:32:06

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

代码语言:javascript
复制
const popularBookGenreDB = async (genre) => {
    try {
        const enw = await Books.find({'genres': {'$all':[genre]} })
        .sort({ bbeScore: -1 })
        .limit(2);

        return enw;
    } catch (error) {
        throw error;
    }};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72776738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档