MongoDB: How to query for records where field is null or not set?

前端 未结 6 1947
鱼传尺愫
鱼传尺愫 2020-11-27 12:58

I have an Email document which has a sent_at date field:

{
  \'sent_at\': Date( 1336776254000 )
}

If this E

6条回答
  •  暖寄归人
    2020-11-27 13:38

    Use:

    db.emails.count({sent_at: null})
    

    Which counts all emails whose sent_at property is null or is not set. The above query is same as below.

    db.emails.count($or: [
      {sent_at: {$exists: false}},
      {sent_at: null}
    ])
    

提交回复
热议问题