MongoDB批量更新|按條件更新SQL|批量刪除某個字段


問題

今天在配置數據的時候,發現之前數據庫字段錯了,有的叫sort_id有的叫SortId,在獲取數據排序的時候無法統一使用。不想一個一個文檔去修改,太多了,搜索到了下面的SQL。
批量按條件更新

db.getCollection('sys_meta_join_table').find({
    "is_active": "N"
}).forEach(
    function(item) {
        db.getCollection('sys_meta_join_table').update({
            "_id": item._id
        }, {
            $set: {
                "is_active": "Y"
            }
        })
    }
)
如果想要刪除MongoDB中一個document的某個字段,該如何做呢?
db.getCollection('sys_meta_join_table').update({"sort_id":{"$exists":true}},{"$unset":{"sort_id",""}},{multi:true});

刪除sys_meta_join_table表的sort_id字段。

模版
db.表.update({"field1":{"$exists":true}},{"$unset":{"field1",""}},{multi:true})
$exists:判斷存在該字段。
注意在后面需要加上multi:true,刪除多行。

參考

https://www.cnblogs.com/wolf-sun/p/5972777.html
https://www.cnblogs.com/mfser/p/13176904.html

原文地址:https://www.525600.xyz/archives/41.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM