mongo数组修改器—$push、$ne、$addtoset、$pop、$pull

这几个方法也很有意思

$push

像已有的数组末尾加入一个元素,要是元素不存在,就会创建一个新的元素,如果元素存在了,就会再添加一个一模一样的元素,会造成元素的重复,所以在使用的时候,要确保该元素不存在

元数据

{
    "_id" : ObjectId("5be8e73cba5ba9887ff2f8c4"),
    "Level" : "本科",
    "ProfessionalName" : "数学类",
    "Scope" : [ 
        "物理"
    ],
    "ContainsProfessional" : [ 
        "数学与应用数学", 
        "统计学", 
        "数学与应用数学(拔尖实验班)"
    ],
    "ScopeStatus" : 1,
    "SchoolMid" : "5b3e0926c26dec21807ec5c5"
}

db.getCollection('TestData').update({"_id" : ObjectId("5be8e73cba5ba9887ff2f8c4")},{"$push":{"Scope":"物理"}})

结果

{
    "_id" : ObjectId("5be8e73cba5ba9887ff2f8c4"),
    "Level" : "本科",
    "ProfessionalName" : "数学类",
    "Scope" : [ 
        "物理", 
        "物理"
    ],
    "ContainsProfessional" : [ 
        "数学与应用数学", 
        "统计学", 
        "数学与应用数学(拔尖实验班)"
    ],
    "ScopeStatus" : 1,
    "SchoolMid" : "5b3e0926c26dec21807ec5c5"
}

如果想要用$push实现不重复更新,可以和$ne结合使用,

$ne 不在

物理不在Scope数组里,才更新

db.getCollection('TestData').update({"_id" : ObjectId("5be8e73cba5ba9887ff2f8c4"),"Scope":{"$ne":"物理"}},{"$push":{"Scope":"物理"}})

$addtoset

往数组里面加入数据,如果数组里已经存在,则不会加入(避免重复)

addToSet"和"addToSet"和"each”组合起来,可以添加多个不同的值

db.getCollection('TestData').update({"_id" : ObjectId("5be8e73cba5ba9887ff2f8c4")},{"$addToSet":{"Scope":{"$each":["物理","化学"]}}})

返回:Updated 1 existing record(s) in 1ms 一毫秒更新了

 

**将”addToSet"和"addToSet"和"each”组合起来,可以添加多个不同的值,二用”ne"和"ne"和"push”组合就不能实现** 

# 原数据
{
    "_id" : ObjectId("5be8e73cba5ba9887ff2f8c4"),
    "Level" : "本科",
    "ProfessionalName" : "数学类",
    "Scope" : [ 
        "物理"
    ],
    "ContainsProfessional" : [ 
        "数学与应用数学", 
        "统计学", 
        "数学与应用数学(拔尖实验班)"
    ],
    "ScopeStatus" : 1,
    "SchoolMid" : "5b3e0926c26dec21807ec5c5"
}


# 修改后
{
    "_id" : ObjectId("5be8e73cba5ba9887ff2f8c4"),
    "Level" : "本科",
    "ProfessionalName" : "数学类",
    "Scope" : [ 
        "物理", 
        "化学"
    ],
    "ContainsProfessional" : [ 
        "数学与应用数学", 
        "统计学", 
        "数学与应用数学(拔尖实验班)"
    ],
    "ScopeStatus" : 1,
    "SchoolMid" : "5b3e0926c26dec21807ec5c5"
}

$pop

删除数组元素,只能从头部或尾部删除一个元素

{
    "_id" : ObjectId("5be8e73cba5ba9887ff2f8c4"),
    "Level" : "本科",
    "ProfessionalName" : "数学类",
    "Scope" : [ 
        "物理", 
        "化学"
    ],
    "ContainsProfessional" : [ 
        "数学与应用数学", 
        "统计学", 
        "数学与应用数学(拔尖实验班)"
    ],
    "ScopeStatus" : 1,
    "SchoolMid" : "5b3e0926c26dec21807ec5c5"
}

从Scope数组中末尾开始删除

db.getCollection('TestData').update({"_id" : ObjectId("5be8e73cba5ba9887ff2f8c4")},{"$pop":{"Scope":1}})

{
    "_id" : ObjectId("5be8e73cba5ba9887ff2f8c4"),
    "Level" : "本科",
    "ProfessionalName" : "数学类",
    "Scope" : [ 
        "物理"
    ],
    "ContainsProfessional" : [ 
        "数学与应用数学", 
        "统计学", 
        "数学与应用数学(拔尖实验班)"
    ],
    "ScopeStatus" : 1,
    "SchoolMid" : "5b3e0926c26dec21807ec5c5"
}

从Scope数组中头部开始删除

db.getCollection('TestData').update({"_id" : ObjectId("5be8e73cba5ba9887ff2f8c4")},{"$pop":{"Scope":-1}})

$pull

删除数组元素,将所有匹配的元素删除。

{
    "_id" : ObjectId("5be8e73cba5ba9887ff2f8c4"),
    "Level" : "本科",
    "ProfessionalName" : "数学类",
    "Scope" : [ 
        "物理",
        "物理",
        "化学"
    ],
    "ContainsProfessional" : [ 
        "数学与应用数学", 
        "统计学", 
        "数学与应用数学(拔尖实验班)"
    ],
    "ScopeStatus" : 1,
    "SchoolMid" : "5b3e0926c26dec21807ec5c5"
}

想要删除物理

db.getCollection('TestData').update({"_id" : ObjectId("5be8e73cba5ba9887ff2f8c4")},{"$pull":{"Scope":"物理"}})

{
    "_id" : ObjectId("5be8e73cba5ba9887ff2f8c4"),
    "Level" : "本科",
    "ProfessionalName" : "数学类",
    "Scope" : [ 
        "化学"
    ],
    "ContainsProfessional" : [ 
        "数学与应用数学", 
        "统计学", 
        "数学与应用数学(拔尖实验班)"
    ],
    "ScopeStatus" : 1,
    "SchoolMid" : "5b3e0926c26dec21807ec5c5"
}

注意:会把符合条件的所有元素都删掉

管道中$push和$addToSet

$push

在结果文档中插入值到一个数组中。

db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}])

$addToSet

在结果文档中插入值到一个数组中,但不创建副本。

db.mycol.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}])

举例:

三条数据

{
    "_id" : ObjectId("5be8e73cba5ba9887ff2f8c4"),
    "Level" : "本科",
    "ProfessionalName" : "数学类",
    "Scope" : [ 
        "化学"
    ],
    "ContainsProfessional" : [ 
        "数学与应用数学", 
        "统计学", 
        "数学与应用数学(拔尖实验班)"
    ],
    "ScopeStatus" : 1,
    "SchoolMid" : "5b3e0926c26dec21807ec5c5"
}

/* 2 */
{
    "_id" : ObjectId("5be8e7a9ba5ba9887ff2f8d5"),
    "Level" : "本科",
    "ProfessionalName" : "社会科学试验班",
    "Scope" : [],
    "ContainsProfessional" : [ 
        "社会学", 
        "经济学(实验班)", 
        "国际经济与贸易(瀚德实验班)", 
        "金融学", 
        "会计学", 
        "人力资源管理", 
        "公共事业管理", 
        "法学(瀚德实验班)", 
        "法学(卓越实验班)", 
        "工商管理(商务实验班)"
    ],
    "ScopeStatus" : 0,
    "SchoolMid" : "5b3e0926c26dec21807ec5c5"
}

/* 3 */
{
    "_id" : ObjectId("5be8e85dba5ba9887ff2f8ec"),
    "Level" : "本科",
    "ProfessionalName" : "物理学类",
    "Scope" : [ 
        "物理"
    ],
    "ContainsProfessional" : [ 
        "天文学", 
        "计算机科学与技术", 
        "电子信息科学与技术", 
        "管理科学", 
        "信息管理与信息系统", 
        "物理学(协同创新实验班)", 
        "物理学(基地班)", 
        "物理学(拔尖实验班)"
    ],
    "ScopeStatus" : 1,
    "SchoolMid" : "5b3e0926c26dec21807ec5c5"
}
View Code

$push

在结果文档中插入值到一个数组中

db.getCollection('TestData').aggregate([{"$match" :{"Level":"本科"}}, {"$project":{"Level":1, "ProfessionalName":1, "Scope":1}}, {"$group":{ "_id":"$Level", "schedule":{"$push":{"leave":"$Level","professionalName":"$ProfessionalName","scope":"$Scope"}}}}])

 结果

{
    "_id" : "本科",
    "schedule" : [ 
        {
            "leave" : "本科",
            "professionalName" : "数学类",
            "scope" : [ 
                "化学"
            ]
        }, 
        {
            "leave" : "本科",
            "professionalName" : "社会科学试验班",
            "scope" : []
        }, 
        {
            "leave" : "本科",
            "professionalName" : "物理学类",
            "scope" : [ 
                "物理"
            ]
        }
    ]
}

$addToSet

在结果文档中插入值到一个数组中,但不创建副本。

db.getCollection('TestData').aggregate([{"$match" :{"Level":"本科"}}, {"$project":{"Level":1, "ProfessionalName":1, "Scope":1}}, {"$group":{ "_id":"$Level", "schedule":{"$addToSet":{"leave":"$Level","professionalName":"$ProfessionalName","scope":"$Scope"}}}}])

结果

{
    "_id" : "本科",
    "schedule" : [ 
        {
            "leave" : "本科",
            "professionalName" : "物理学类",
            "scope" : [ 
                "物理"
            ]
        }, 
        {
            "leave" : "本科",
            "professionalName" : "社会科学试验班",
            "scope" : []
        }, 
        {
            "leave" : "本科",
            "professionalName" : "数学类",
            "scope" : [ 
                "化学"
            ]
        }
    ]
}

肉眼观察似乎除了展示顺序有变化,其他貌似没有变化

相关文档

https://www.cnblogs.com/zzy-9318/p/10022651.html

 

posted on 2018-12-26 17:13  V神丫丫  阅读(1886)  评论(0编辑  收藏  举报