MongoDB 子字符串数组和聚合管道

时间:2021-04-30 12:45:55

标签: mongodb aggregation-framework

我正在使用 MongoDB 聚合管道,如您所见,我过滤了电影收藏,以寻找获得奥斯卡奖的电影。但是,由于奖励字符串包含的信息比我需要的要多,因此我将其拆分。

现在,我不知道我只能保留等于获得奥斯卡奖金额的子字符串。

谁能帮我一把?

db.movies.aggregate([{$match : {"awards" : /Won . Oscar*/}}, {$sort: {"awards" : -1}}, {$project : {title : 1 , number_of_oscars:{$split:["$awards", "."]}, _id : 0} }]).pretty()

Array of substrings

我想要的是:

{
"title" : "The Best Years of Our Lives ,
"number_of_oscars" : "Won 7 Oscars"

}

数据结构:



{
        {
        "_id" : ObjectId("573a1393f29313caabcdd0e5"),
        "title" : "The Best Years of Our Lives",
        "year" : 1946,
        "runtime" : "The Best Years of Our Lives",
        "released" : ISODate("1947-06-17T00:00:00Z"),
        "cast" : [
                "Fredric March",
                "Dana Andrews",
                "Teresa Wright",
                "Myrna Loy"
        ],
        "poster" : "http://ia.media-imdb.com/images/M/MV5BMTk1NTAxNzg3Nl5BMl5BanBnXkFtZTcwNjU4OTQwNw@@._V1_SX300.jpg",
        "plot" : "Three WWII veterans return home to small-town America to discover that they and their families have been irreparably changed.",
        "fullplot" : "The story concentrates on the social re-adjustment of three World War II servicemen, each from a different station of society. Al Stephenson returns to an influential banking position, but finds it hard to reconcile his loyalties to ex-servicemen with new commercial realities. Fred Derry is an ordinary working man who finds it difficult to hold down a job or pick up the threads of his marriage. Having had both hands burnt off during the war, Homer Parrish is unsure that his fianc�e's feelings are still those of love and not those of pity. Each of the veterans faces a crisis upon his arrival, and each crisis is a microcosm of the experiences of many American warriors who found an alien world awaiting them when they came marching home.",
        "awards" : "Won 7 Oscars. Another 14 wins & 1 nomination.",
        "lastupdated" : "2015-09-17 01:24:38.307000000",
        "type" : "movie",
        "languages" : [
                "English"
        ],
        "directors" : [
                "William Wyler"
        ],
        "writers" : [
                "Robert E. Sherwood (screen play)",
                "MacKinlay Kantor (from a novel by)"
        ],
        "imdb" : {
                "rating" : 8.2,
                "votes" : 36573,
                "id" : 36868
        },
        "countries" : [
                "USA"
        ],
        "rated" : "PASSED",
        "genres" : [
                "Drama",
                "War",
                "Romance"
        ],
        "tomatoes" : {
                "viewer" : {
                        "rating" : 4.2,
                        "numReviews" : 10304,
                        "meter" : 92
                },
                "dvd" : ISODate("1997-10-30T00:00:00Z"),
                "rotten" : 1,
                "lastUpdated" : ISODate("2015-09-17T17:19:13Z"),
                "consensus" : "An engrossing look at the triumphs and travails of war veterans, The Best Years of Our Lives is concerned specifically with the aftermath of World War II, but its messages speak to the overall American experience.",
                "critic" : {
                        "rating" : 8.6,
                        "numReviews" : 46,
                        "meter" : 98
                },
                "production" : "RKO Radio Pictures",
                "fresh" : 45
        },
        "num_mflix_comments" : 3,
        "comments" : [
                {
                        "name" : "April Hunt",
                        "email" : "april_hunt@fakegmail.com",
                        "movie_id" : ObjectId("573a1393f29313caabcdd0e5"),
                        "text" : "Inventore odio dolor quis provident maiores cum. Eum eaque placeat a dolores molestias tenetur. Velit velit adipisci quo eum corrupti.",
                        "date" : ISODate("2011-02-12T01:16:25Z")
                },
                {
                        "name" : "Larry Smith Jr.",
                        "email" : "larry_smith_jr.@fakegmail.com",
                        "movie_id" : ObjectId("573a1393f29313caabcdd0e5"),
                        "text" : "Dolores facere vero saepe harum. Voluptas molestias alias magnam harum reiciendis eveniet. Eum molestiae facilis eveniet ex cumque harum.",
                        "date" : ISODate("2002-03-26T09:23:37Z")
                },
                {
                        "name" : "Dana Cross",
                        "email" : "dana_cross@fakegmail.com",
                        "movie_id" : ObjectId("573a1393f29313caabcdd0e5"),
                        "text" : "Quae reprehenderit veritatis hic ex neque. Ad vero corrupti dolorem dolor optio voluptatum occaecati. Beatae fuga vero impedit iste. Eligendi ratione perferendis velit deleniti sit.",
                        "date" : ISODate("1970-07-17T05:42:03Z")
                },
                {
                        "name" : "Gregor Clegane",
                        "email" : "hafthór_júlíus_björnsson@gameofthron.es",
                        "movie_id" : ObjectId("573a1393f29313caabcdd6b8"),
                        "text" : "Porro nihil maxime magnam inventore placeat ad sequi. Placeat ullam officia delectus pariatur ab fuga aspernatur praesentium. Laudantium omnis ducimus quibusdam enim vel vitae.",
                        "date" : ISODate("1983-02-09T05:47:19Z")
                }
        ],
        "metacritic" : null,
        "num_mflix_commenents" : 4
}

谢谢

0 个答案:

没有答案