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
相关产品推荐
相关产品推荐

