假定需要查询name中包含s的:

db.customer.find({'name': /.*s.*/i})

pymongo的写法如下:

rexExp = re.compile('.*s.*', re.IGNORECASE)
db.customer.find({'name':rexExp })

或者

db.customer.find({'name': {'$regex':'.*s.*'} })

 

为提高查询效率,添加索引{'name':1}

 

测试结果:无法命中索引

 

要想命中索引,还得改下查询条件,查询必须符合2点

  1. 必须是s开头的,而不能是s在中间
  2. 必须是大小写敏感的

 

那么,查询看起来应该是这个样子:

db.customer.find({'name': /^s.*/})

pymongo的写法如下:

rexExp = re.compile('^s.*')
db.customer.find({'name':rexExp })

 

omg,看来单靠mongodb是很难满足实际的应用场景,所以还是另外考虑其它搜索引擎做为全文索引的补充吧,lucene系列(solr,elasticSearch)或sphinx都可以考虑下


 

 

 

 

 

 

 

 

 

 

 

相关文章:

  • 2022-12-23
  • 2021-04-16
  • 2021-08-05
  • 2022-12-23
  • 2022-12-23
  • 2022-01-25
  • 2021-09-27
  • 2021-06-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-03
  • 2021-09-27
  • 2022-01-31
  • 2022-12-23
相关资源
相似解决方案