Find all docs where field doesn't exists, plus if field exists apply condition

前端 未结 1 1501
面向向阳花
面向向阳花 2021-01-11 16:53

Is there a way to make a query that:

  • if certain field exists, it will apply condition on the field and if it pass, it will add return document and add it to th
相关标签:
1条回答
  • 2021-01-11 17:00

    How about something like this:

    db.stackoverflow.find({
      $or: [
        { howmuch: { $exists:false } },
        { howmuch:5 }
      ]})
    

    In the stackoverflow collection, this will find all documents that do not have the howmuch field plus all documents that do have howmuch set to 5.

    0 讨论(0)
提交回复
热议问题