V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐工具
RoboMongo
推荐书目
50 Tips and Tricks for MongoDB Developers
Related Blogs
Snail in a Turtleneck
Ewig
V2EX  ›  MongoDB

mongodb 里面如何把 null 元素删除

  •  
  •   Ewig · 2019-01-16 15:58:45 +08:00 · 7734 次点击
    这是一个创建于 2136 天前的主题,其中的信息可能已经有所发展或是发生改变。
    > 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 ()什么的能返回个数
    2 条回复    2019-01-16 16:29:15 +08:00
    calpamomo
        1
    calpamomo  
       2019-01-16 16:23:03 +08:00
    laoluo1991
        2
    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"
    }
    }
    }])
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3717 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 10:32 · PVG 18:32 · LAX 02:32 · JFK 05:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.