有没有办法在mongo DB中获取json数据的内部值?

时间:2014-10-08 12:18:57

标签: mongodb

我的Json数据是这样的(这就是我存储在mongo DB中的内容)

/* 0 */
{
    "_id" : "e9925559-4830-476f-8d6a-789ade4ec51c",
    "account_user_id" : "sahaal",
    "account_number" : "23423",
    "Job Profile" : {
        "externalid" : "250250ICM",
        "bgcpackagetype" : [ 
            {
                "id" : "C19902",
                "value" : "1800 - Happy Path Background Check",
                "formattedvalue" : "1800 - Happy Path Background Check"
            }, 
            {
                "id" : "C19900",
                "value" : "2112 - Esteem Validation Package",
                "formattedvalue" : "2112 - Esteem Validation Package"
            }
        ],
        "links" : [ 
            {
                "title" : "The current profile being viewed.",
                "rel" : "self",
                "url" : "https://api.icims.com/customers/2187/jobs/2015"
            }
        ],
        "jobid" : "2013-2015"
    },
    "request" : {
        "newStatus" : "9782",
        "oldStatus" : "1752",
        "customerId" : "1234",
        "jobId" : "1242",
        "userId" : "3",
        "personId" : "1364",
        "links" : [ 
            {
                "title" : "Applicant Workflow",
                "rel" : "applicantWorkflow",
                "url" : "https://api.icims.com/customers/1234/applicantworkflows/2203"
            }, 
            {
                "title" : "Job Profile",
                "rel" : "job",
                "url" : "https://api.icims.com/customers/2187/jobs/2015"
            }, 
            {
                "title" : "Person Profile",
                "rel" : "person",
                "url" : "https://api.icims.com/customers/2187/people/7260"
            }, 
            {
                "title" : "Posting User",
                "rel" : "user",
                "url" : "https://api.icims.com/customers/2187/people/7425"
            }
        ]
    },
    "Candidate Profile" : {
        "workexperience" : [ 
            {
                "workdescription" : "Investigated the effects on body weight and bone growth of replacement injections of thyroid hormone and drugs that inhibit thyroid hormone production * Performed IM injections in chickens * Ran 80 blood samples through gas chromatograph to check T1 levels * Prepared bone growth media for 8 sets of experiments",
                "entry" : 47964,
                "workstartdate" : "2011-01-01",
                "workenddate" : "2011-06-01",
                "worktitle" : "Research Assistant",
                "workemployer" : "UC Riverside"
            }, 
            {
                "workcity" : "Pomona",
                "workdescription" : "Worked closely with staff performing tasks such as escorting patients, preparing charts, stocking rooms with supplies, running errands, answering phones, and light clerical tasks * Provided patient care by taking vital signs, weighing patients, answering call lights and providing psychosocial support for patients * Learned basics of telemetry",
                "entry" : 47965,
                "workstartdate" : "2009-05-01",
                "workcountry" : {
                    "id" : "D41001",
                    "value" : "United States",
                    "abbrev" : "US"
                },
                "workenddate" : "2011-06-01",
                "worktitle" : "Intern",
                "workstate" : {
                    "id" : "D41001008",
                    "value" : "California",
                    "abbrev" : "CA"
                },
                "workemployer" : "Pomona Valley Hospital Medical Center"
            }
        ],
        "licensecertification" : [ 
            {
                "stateissued" : {
                    "id" : "D41001008",
                    "value" : "United States - California",
                    "formattedvalue" : "United States - California"
                },
                "entry" : 48064,
                "licensecertstatus" : {
                    "id" : "D37002036001",
                    "value" : "Current",
                    "formattedvalue" : "Current"
                },
                "licensenumber" : "D8257463",
                "licensetype" : "Drivers License"
            }
        ],
        "email" : "ravi.sathish@fadv.com",
        "birthdate" : "1970-01-01",
        "lastname" : "Landis",
        "links" : [ 
            {
                "title" : "The current profile being viewed.",
                "rel" : "self",
                "url" : "https://api.icims.com/customers/2187/people/7260"
            }
        ],
        "firstname" : "Job",
        "addresses" : [ 
            {
                "addresstype" : {
                    "id" : "D84002",
                    "value" : "Home",
                    "formattedvalue" : "Home"
                },
                "addresscountry" : {
                    "id" : "D41001",
                    "value" : "United States",
                    "abbrev" : "US"
                },
                "addresszip" : "92521",
                "addressstreet1" : "1234 Street Name",
                "entry" : 3403,
                "addressstate" : {
                    "id" : "D41001008",
                    "value" : "California",
                    "abbrev" : "CA"
                },
                "addresscity" : "Riverside"
            }
        ],
        "education" : [ 
            {
                "degree" : {
                    "id" : "C19850",
                    "value" : "B.S",
                    "formattedvalue" : "B.S"
                },
                "school" : {
                    "id" : "C19849",
                    "value" : "University of California",
                    "formattedvalue" : "University of California"
                },
                "entry" : 289,
                "major" : {
                    "id" : "C19852",
                    "value" : "Biological Sciences",
                    "formattedvalue" : "Biological Sciences"
                }
            }
        ],
        "phones" : [ 
            {
                "phonetype" : {
                    "id" : "D83002",
                    "value" : "Home",
                    "formattedvalue" : "Home"
                },
                "phonenumber" : "555-555-5555",
                "entry" : 4823
            }, 
            {
                "phonetype" : {
                    "id" : "D83003",
                    "value" : "Fax",
                    "formattedvalue" : "Fax"
                },
                "phonenumber" : "666-555-6666",
                "entry" : 4829
            }
        ]
    },
    "account_password" : "Pass12!$",
    "Recruiter Profile" : {
        "email" : "Harry@fadv.com",
        "lastname" : "HiringManager",
        "links" : [ 
            {
                "title" : "The current profile being viewed.",
                "rel" : "self",
                "url" : "https://api.icims.com/customers/2187/people/7171"
            }
        ],
        "firstname" : "Harry"
    }
}

搜索结果应仅显示

"links" : [ 
                {
                    "title" : "Applicant Workflow",
                    "rel" : "applicantWorkflow",
                    "url" : "https://api.icims.com/customers/1234/applicantworkflows/2203"
                }, 
                {
                    "title" : "Job Profile",
                    "rel" : "job",
                    "url" : "https://api.icims.com/customers/2187/jobs/2015"
                }, 
                {
                    "title" : "Person Profile",
                    "rel" : "person",
                    "url" : "https://api.icims.com/customers/2187/people/7260"
                }, 
                {
                    "title" : "Posting User",
                    "rel" : "user",
                    "url" : "https://api.icims.com/customers/2187/people/7425"
                }
            ]

编辑: -

我的需要是,我需要url"标题" ="人物简介"。因为它可以在任何位置,所以我可以说第三个网址

1 个答案:

答案 0 :(得分:2)

您可以通过点表示法获取数组子文档,以便

中的投影"request.links":1,_id:0
db.collection.find({query},{"request.links":1,_id:0})

应该这样做。


编辑后的问题:

对于编辑过的问题,您需要汇总:

db.collection.aggregate( [{$match: {Your document query}},
                          {$unwind: "$request.links"},
                          {$match: {"request.links.title":"Person Profile"}},
                          {$project: {_id : 0, link : "$request.links.url"}}])

逐步解释:

  1. 首先$match找到您的文档(与查找相同)
  2. $unwind为每个数组元素生成一个文档
  3. 第二个$match搜索带有标题的链接
  4. $project仅显示链接