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

标签 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" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e29083948bcc9183968a8b918aa284838694cc818d8f" rel="noreferrer noopener nofollow">[email protected]</a>",
        "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" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="531b3221212a13353237257d303c3e" rel="noreferrer noopener nofollow">[email protected]</a>",
        "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"
                }
            ]

编辑:-

我的需求是,我需要“title”=“Person Profile”的url。因为它可以在任何位置,所以我可以说第三个网址

最佳答案

您可以通过点表示法获取数组子文档,因此投影 "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 查找您的文档(与 find 相同)
  2. $unwind 为每个数组元素创建一个文档
  3. 第二个 $match 搜索包含您标题的链接
  4. $project 仅显示链接

关于mongodb - mongo DB中有没有办法获取json数据的内部值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26256735/

相关文章:

javascript - 如何根据Js数组中保存的Id获取分散在数组字段中的Object

ruby - 尝试使用 AGGREGATE 时 FieldPath 字段名称可能不包含 '.'

javascript - 如何在 Node.js 中根据 UserId 获取匹配的聚合

mongodb - 使用 $lookup mongodb 填充到深层

c# - MongoDB 从子文档中删除子文档文档

Python 字典 : removing u' chars

java - 从 Gson 到 MongoDB 对象的转换器

java - Spring Data MongoDB : MergeOperation returns the whole collection. 为什么?

node.js - 计算 mongodb 中不同值的数量,然后计算总和

mongodb - 根据mongodb中另一个数组字段的值将数组字段添加到集合中