代码之家  ›  专栏  ›  技术社区  ›  Ghazanfar Khan

MongoDB find all的值未定义

  •  1
  • Ghazanfar Khan  · 技术社区  · 5 年前

    嗨,我在nosqlide中编写了一些vanilajavascript,不小心更新了mongodb中的一些记录,其值为undefined。问题是记录存在,但当我运行$exists为true时,它在Robo3T中显示记录,但在NoSQLBooster中不显示。然而,当我运行电子邮件等于未定义它不返回任何记录,但记录存在。下面是示例查询和结果截图

     db.customer.find({source:'some value',"email":{$exists: true}})
       .projection({})
       .sort({_id:-1})
    

    enter image description here

    0 回复  |  直到 5 年前
        1
  •  1
  •   JohnnyHK    5 年前

    您可以查询 email undefined 通过使用 $type 操作员:

    db.customer.find({source: 'some value', email: {$type: 'undefined'}})
    

    请注意,未定义的类型在中列为已弃用 the docs ,但目前仍然有效。

    查询 email: null 也会退回这些文件,但其中也包括 电子邮件 缺少或设置为 null . 因此,您需要使用另外两个术语来进一步限定查询,以排除这些术语:

    db.customer.find({$and: [
        {source: 'some value'},
        {email: null}, 
        {email: {$not: {$type: 'null'}}}, 
        {email: {$exists: true}}]})