douqian9729 2018-04-13 11:30
浏览 48
已采纳

将特定的退货查询转换为mgo

I have a query which returns all names from a collection's documents which contain a specific text. In the following example, return all names which contain the sequence "oh" case-insensitively; do not return other fields in the document:

find({name:/oh/i}, {name:1, _id:0})

I have tried to translate this query into mgo:

Find([]bson.M{bson.M{"name": "/oh/i"}, bson.M{"name": "1", "_id": "0"}})

but there are always zero results when using mgo. What is the correct syntax for such a query using mgo?

This question is different from the alleged duplicates because none of those questions deal with how to restrict MongoDB to return only a specific field instead of entire documents.

  • 写回答

1条回答 默认 最新

  • dongyun6835 2018-04-13 12:43
    关注

    To execute queries that use regexp patterns for filtering, use the bson.RegEx type.

    And to exclude fields from the result documents, use the Query.Select() method.

    Like in this example:

    c.Find(bson.M{"name": bson.RegEx{Pattern: "oh", Options: "i"}}).
        Select(bson.M{"name": 1, "_id": 0})
    

    Translation of the regexp:

    name:/oh/i
    

    This means to match documents where the name field has a value that contains the "oh" sub-string, case insensitive. This can be represented using a bson.RegEx, where the RegEx.Pattern field gets the pattern used in the above expression ("oh"). And the RegEx.Options may contain options now to apply / match the pattern. The doc lists the possible values. If the Options field contains the 'i' character, that means to match case insensitive.

    If you have a user-entered term such as "[a-c]", you have to quote regexp meta characters, so the final pattern you apply should be "\[a-c\]" To do that easily, use the regexp.QuoteMeta() function, e.g.

    fmt.Println(regexp.QuoteMeta("[a-c]")) // Prints: \[a-c\]
    

    Try it on the Go Playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?