如何使用MongoDB shell从MongoDB集合中获取特定字段?

时间:2017-04-24 23:42:04

标签: json mongodb shell

我已在集合中的Mongodb索引了一个json文件" clicklog"使用shell命令。以下是我的shell命令的结果:

db.clicklogs.find().pretty()

输出:

    {
            "_id" : ObjectId("58fe78dcfbe21fa7896552e8"),
            "preview" : false,
            "offset" : 0,
            "result" : {
                    "search_term" : "484797",
                    "request_time" : "Sat Apr 01 23:58:49 -0400 2017",
                    "request_ip" : "127.0.0.1",
                    "stats_type" : "clickstats",
                    "upi" : "66024330304",
                    "unit" : "CITCS",
                    "job_title" : "IT Engineer",
                    "vpu" : "ICR",
                    "organization" : "73",
                    "location" : "MH",
                    "city" : "San Diego",
                    "country" : "USA",
                    "title" : "TOM",
                    "tab_name" : "People-Tab",
                    "page_name" : "PEOPLE",
                    "result_number" : "1",
                    "page_num" : "0",
                    "session_id" : "14e88b44576ad4fdc035bc41529762ad1",
                    "total_results" : "1",
                    "_raw":"request_time=Sat Apr 01 23:58:49 -0400 2017,request_ip=127.0.0.1,application=Search,stats_type=clickstats,upi=660243301304,unit=CITCS,job_title=IT Assistant, Client Services,vpu=ICR,location=DHAKA, BANGLADESH (IFC),organization=73,city=Dhaka,country=BANGLADESH,city_code=,search_term=484797,title=   Tom,url=http://isearch.worldbank.org/skillfinder/ppl_profile_new/000484797,tab_name=People-Tab,page_name=PEOPLE,result_number=1,page_num=0,filter=qterm=484797,total_results=1,app_environment=production,log_version=1.0,session_id=4e88b44576ad4fdc035bc41529762ad1",
                    "_time":"2017-04-01T23:58:49.000-0400"

            }
    }
{"_id" : ObjectId("58fe78dcfbe21fa7896552e9"),
        "preview" : false,
        "offset" : 0,
         "result" : {
                "search_term" : "demo",
                "request_time" : "Sat Apr 01 23:58:49 -0400 2017",
                "request_ip" : "127.0.0.1",
                 ....
                 "time":"2017-04-01T23:58:49.000-0400"
}
}

对于每个json文档,我只想获得几个字段(id,searchterm,upi,page_name,sessionid,url(在_raw下))。是否可以使用mongo shell命令并将结果文档存储在新集合中?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

您可以在3.4版本中尝试以下聚合。

该查询多次使用$split运算符来达到url值。休息是标准投影领域。

$out阶段将结果写入新集合。

db.getCollection('clicklogs').aggregate([{
        $project: {
            searchterm: "$result.searchterm",
            upi: "$result.upi",
            page_name: "$result.page_name",
            session_id: "$result.session_id",
            url: {
                $let: {
                    vars: {
                        obj: {
                            $arrayElemAt: [{
                                $split: ["$result._raw", ',']
                            }, 1]
                        }
                    },
                    in: {
                        $arrayElemAt: [{
                            $split: ["$$obj", '=']
                        }, 1]
                    }
                }
            }
        }
    },
    {
        $out: "clicklogs_temp"
    }
])