推荐工具
RoboMongo
推荐书目
50 Tips and Tricks for MongoDB Developers
Related Blogs
Snail in a Turtleneck
stargazer
V2EX  ›  MongoDB

请教在 mongo 中如何对一个列表字段的内容进行模糊查询?

  •  
  •   stargazer · May 29, 2015 · 5052 views
    This topic created in 4083 days ago, the information mentioned may be changed or developed.

    RT,请教各位大神,如何对一个list的字段内容进行模糊查询,thx

    6 replies    2015-05-30 10:28:29 +08:00
    cevincheung
        1
    cevincheung  
       May 30, 2015
    只能用正则了
    kongkongyzt
        2
    kongkongyzt  
       May 30, 2015 via iPhone
    同问
    cevincheung
        3
    cevincheung  
       May 30, 2015   ❤️ 1
    @kongkongyzt
    @stargazer

    正则
    db.collection.find({field :/^A/})
    stargazer
        4
    stargazer  
    OP
       May 30, 2015
    @cevincheung
    list的也是么?
    karloku
        5
    karloku  
       May 30, 2015 via iPhone   ❤️ 1
    @stargazer Array也一样, mongo对Array字段搜索不加操作符的话就是匹配任一的元素

    但是正则查询只有固定字符串头的才能使用index, 这点和大部分sql的like是一样的. 需要完全模糊查询话还是下面几种方案
    1 用textIndex, 这个方便但是现在不支持中文
    2 把字段分词以后专门存一个新字段来索引
    3 用专门的搜索引擎
    stargazer
        6
    stargazer  
    OP
       May 30, 2015
    @karloku thx我明白了,多谢~
    @cevincheung 多谢指点~
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   838 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 20:32 · PVG 04:32 · LAX 13:32 · JFK 16:32
    ♥ Do have faith in what you're doing.