首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从MongoDB文档中获取一个值

从MongoDB文档中获取一个值
EN

Stack Overflow用户
提问于 2016-10-26 15:19:11
回答 1查看 48关注 0票数 1

我有一个集合,其中文档如下(MongoDB):

代码语言:javascript
复制
{
    _id: 'ABPS1001',
    Brand: 'DecNag',
    serial: '2393-829-109',
    resume: [
        {
            status: '1',
            nameAg: 'Gina',
            lastNameAg: 'Saenz',
            coord_id: '1025',
            movDate: '25-10-2016 11:33'
        },
        {
            status: '0',
            techID: '11',
            coord_id: '1025',
            movDate: '30-10-2016 16:29',
            idReplace: 'ABPS1026'
        },
        {
            status: '1',
            nameAg: 'Diana',
            lastNameAg: 'Gutierrez',
            coord_id: '1014',
            techID: '10',
            movDate: '04-11-2016 09:12'
        },
        {
            status: '0',
            techID: '12',
            coord_id: '1014',
            movDate: '30-11-2016 16:25',
            idReplace: 'ABPS1021'
        },
        {
            status: '1',
            nameAg: 'Laura',
            lastNameAg: 'Diaz',
            coord_id: '1012',
            techID: '11',
            movDate: '04-12-2016 11:33'
        },
        {
            status: '0',
            techID: '10',
            coord_id: '1012',
            movDate: '22-12-2016 12:21',
            idReplace: 'ABPS1107'
        },
        {
            status: '1',
            nameAg: '172.27.48.125',
            lastNameAg: '',
            coord_id: '1004',
            techID: '12',
            movDate: '27-12-2016 08:30'
        },
        {
            status: '0',
            techID: '11',
            movDate: '02-02-2017 14:12',
            idReplace: 'ABPS1107'
        }
     ]
 }

我需要从_id:'ABPS1001‘而不是整个文档中获得“简历”上的最后一个条目。有没有办法用MongoDB语句代替编程语言进行处理呢?

此外,我如何在“简历”上的任何一组值中添加或删除值(例如,如果我想将"coord_id“添加到”简历“的最后一组中)?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-26 16:13:54

下面是如何使用聚合来完成问题的第一部分:

代码语言:javascript
复制
// Returns last value of the resume array...
// First - Match the document
// Second - Project to slice the last element of the resume array and put it into a new document called lastValue
db.foo.aggregate([
    { $match: {"_id": "ABPS1001"} }, 
    { $project: { lastValue: { $slice: [ "$resume", -1 ] } } }
])

在以下方面的成果:

代码语言:javascript
复制
{
    "_id" : "ABPS1001",
    "lastValue" : [
        {
            "status" : "0",
            "techID" : "11",
            "movDate" : "02-02-2017 14:12",
            "idReplace" : "ABPS1107"
        }
    ]
}

对于第二部分,您可以使用'$‘运算符请看这里进行位置更新

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40266112

复制
相关文章

相似问题

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