uni-app JQL正则规则字段有下划线_搜索不出数据

uni-app JQL正则规则字段有下划线_搜索不出数据

信息类型 信息内容
产品分类 uniCloud/App

操作步骤:

db.collection('sfield').where({'smodel_id':new RegExp('62')}).get();

预期结果:

搜索有结果

实际结果:

无结果

bug描述:

数据:

{  
    "smodel_id": "627780794324b90001fa281c",  
    "name": "name",  
}

这样可以正常搜索:

db.collection('sfield').where({'name':new RegExp('na')}).get();

这样搜索不出任何数据:

db.collection('sfield').where({'smodel_id':new RegExp('62')}).get();
db.collection('sfield').where({smodel_id:'627780794324b90001fa281c'}).get();
db.collection('sfield').where({smodel_id:new RegExp('627780794324b90001fa281c')}).get();

name字段可以按照正常搜索,smodel_id不可以,都无索引


更多关于uni-app JQL正则规则字段有下划线_搜索不出数据的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

请上传能复现问题的demo

更多关于uni-app JQL正则规则字段有下划线_搜索不出数据的实战教程也可以访问 https://www.itying.com/category-93-b0.html


我也碰到了此问题,请问解决了吗?

遇到相同问题只要是字段中间带下划线的字段如:(parentlocation_id)查询都不正常 查询出来的结果与预期不一致

在使用 uni-app 的 JQL(JavaScript Query Language)进行数据库查询时,如果字段名中包含下划线 _,可能会导致查询不到数据。这是因为在某些情况下,JQL 可能会将下划线 _ 视为特殊字符,从而导致查询条件不匹配。

解决方法

  1. 转义下划线:在 JQL 查询中,如果字段名包含下划线 _,可以尝试对下划线进行转义。使用 \\_ 来表示下划线。

    const query = db.collection('your_collection').where({
      'field\\_name': 'value'
    }).get()
    
  2. 使用点符号:如果字段名是嵌套的,可以使用点符号 . 来访问嵌套字段。

    const query = db.collection('your_collection').where({
      'field.name': 'value'
    }).get()
    
  3. 检查字段名:确保字段名在数据库中确实包含下划线 _,并且没有其他拼写错误。

  4. 使用正则表达式:如果需要进行模糊查询,可以使用正则表达式来匹配字段名。

    const query = db.collection('your_collection').where({
      'field_name': /value/i
    }).get()
    

示例

假设你的集合 your_collection 中有一个字段名为 user_name,你想要查询 user_namejohn_doe 的记录。

const query = db.collection('your_collection').where({
  'user\\_name': 'john_doe'
}).get()

或者使用正则表达式:

const query = db.collection('your_collection').where({
  'user_name': /john_doe/i
}).get()

注意事项

  • 确保数据库中的字段名和查询条件中的字段名完全一致。
  • 如果问题仍然存在,可以尝试使用 db.command 提供的正则表达式命令来进行更复杂的查询。
const _ = db.command
const query = db.collection('your_collection').where({
  'user_name': _.regex({
    regexp: 'john_doe',
    options: 'i'
  })
}).get()
回到顶部