mongodb 里面如何把 null 元素删除

2019-01-16 15:58:45 +08:00
 Ewig
> db.zhipin_cookie.find()[0]
{
"_id" : ObjectId("5c3e89b5cfab5493e7c04e1c"),
"cookie" : [
"_umdata=C234BF9D3AFA6FE77F94838529E088A9BBD97FE15A9C993647E1BA8899EBCF18025B4DFBCA9E6B73CD43AD3E795C914C12AFE595899E431C947FA6E1ED7DEB3B; _uab_collina=154056544968891366026752; JSESSIONID=\"\"; __c=1544930213; __g=-; __l=l=%2Fwww.zhipin.com%2Fjob_detail%2F0fa6383a3ce0cd951XR92dm_E1Q~.html&r=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DmViDrfNXobBzHXLDRAMNB7UvOwByvB88ZEiALvXHu01RdCvB1_SFi8P9S6rfx7H4kc2WoT2-WLQ0-x-A4HFchKcom5EK8ofnYQ7Snen7sbG%26wd%3D%26eqid%3D8b4d317e00010687000000035c15c3a1; lastCity=101030100; Hm_lvt_194df3105ad7148dcf2b98a91b5e727a=1547429880,1547429955,1547444163,1547515045; toUrl=https%3A%2F%2Fwww.zhipin.com%2Fgongsi%2F; Hm_lpvt_194df3105ad7148dcf2b98a91b5e727a=1547601953; __a=85347837.1534956138.1539538585.1544930213.643.4.587.643",
null,
"_uab_collina=154760169922736119562602; JSESSIONID=\"\"; __g=-; Hm_lvt_194df3105ad7148dcf2b98a91b5e727a=1547601699; __c=1547601714; __l=r=&l=%2Fwww.zhipin.com%2Fgongsi%2F; toUrl=https%3A%2F%2Fwww.zhipin.com%2Fgongsi%2F; __a=99734927.1547601718.1547601718.1547601714.6.2.5.6; Hm_lpvt_194df3105ad7148dcf2b98a91b5e727a=1547611972",
"_uab_collina=154761273089397171149706; __guid=95203226.204010856270686820.1547612729977.7249; JSESSIONID=\"\"; __g=-; Hm_lvt_194df3105ad7148dcf2b98a91b5e727a=1547612731; toUrl=https%3A%2F%2Fwww.zhipin.com%2Fgongsi%2F; monitor_count=2; __c=1547612762; __l=r=&l=%2Fwww.zhipin.com%2Fgongsi%2F; __a=606041.1547612728.1547612728.1547612762.2.2.1.2; Hm_lpvt_194df3105ad7148dcf2b98a91b5e727a=1547612762"
]
}

现在这个 cookie 数组里面有个 null 如何把他给删掉?我用 pop 也不行,求解决

还有就是如何查看这个 cookie 里面有多少个,比如 len ()什么的能返回个数
7759 次点击
所在节点    MongoDB
2 条回复
calpamomo
2019-01-16 16:23:03 +08:00
laoluo1991
2019-01-16 16:29:15 +08:00
删除数组内 null 值:

db.zhipin_cookie.update({}, {"$pull":{"cookie":{"$type": 10}}}, false, true)

查询数组大小我知道的方法是在聚合里查询:

db.zhipin_cookie.aggregate([{
"$match": {
"cookie": {
"$exists": true
}
}
},
{
"$project": {
"sizeOfCookie": {
"$size": "$cookie"
}
}
}])

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/527620

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX