您现在的位置是:课程教程文章
mongodb怎么删除所有为空的字段
2023-12-18 18:49课程教程文章 人已围观
-
Node.js全栈之路第三季:MongoDB/Mongoose数据
Node.js全栈之路第三季:MongoDB/Mongoose数据... -
MongoDB 数据库概述
MongoDB 数据库概述本课程将介绍文档型数据库以及MongoDB的基础概念。课程将比较关系型数据库和... -
【赵强老师】使用MongoDB的Web控制台
【赵强老师】使用MongoDB的Web控制台什么是MongoDB? MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在... -
【赵强老师】MongoDB中的索引
【赵强老师】MongoDB中的索引索引是提高查询查询效率最有效的手段。 索引是一种特殊的数据结构,索引以...
1、直接使用下面的代码删除所有为空的字段:
//run in mongo shell var coll = db.getCollection("collectionName"); var cursor = coll.find(); while (cursor.hasNext()) { var doc = cursor.next(); var keys = {}; var hasNull = false; for ( var x in doc) { if (x!="_id" && doc[x] == null) { keys[x] = 1; hasNull = true; } } if (hasNull) { coll.update({_id: doc._id}, {$unset:keys}); } }
通过循环遍历所有字段获取其中为空的字段,然后使用update()方法删除所有为空的字段即可。
下一篇:没有了