You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MongoDB的find()方法是否支持使用通配符?

MongoDB find() 匹配Destination字段所有内容的正确方式

你不能直接用{ Destination : "wildcard?" }这种写法实现通配符匹配——MongoDB会把这个当成精确匹配字符串"wildcard?",达不到返回所有Destination字段内容的效果。要实现需求,有几种常用方法:

  • 查询所有包含Destination字段的文档(不管字段值是什么,甚至是null):
    使用$exists操作符:

    db.collection.find({ Destination: { $exists: true } })
    
  • 查询包含非null的Destination字段的文档:
    结合$exists和$ne过滤掉null值:

    db.collection.find({ Destination: { $exists: true, $ne: null } })
    
  • 用正则匹配任意非空的Destination值:
    如果要确保字段有实际内容(不是空字符串或null),可以用正则表达式:

    db.collection.find({ Destination: /.+/ })
    

    要是允许匹配空字符串,就用/.*/,不过这种情况和$exists: true效果基本一致。

内容的提问来源于stack exchange,提问作者Robert Green

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 11:57:17