The mgo regular expression does not work

Now I have several documents, each of which has a key pathand a value of type \A\, \B\, \A\C\, \A\C\D\, \A\E\, \A\E\F\.

I want to find those that have only 1 segment. This means that the result should be \A\and \B\. I use a regex /^\\[^\\]*\\$/that works fine in the MongoDB terminal. But when I tried to apply it to Go programs, this would not work.

Code Code:

var nodeList []NodeEntry // NodeEntry would match every field of one document
err = c.Find(bson.M{"path": bson.M{"$regex": bson.RegEx{"^\\[^\\]*\\$", ""}}}).All(&nodeList)
fmt.Println(nodeList)

Output:

[]

This is so strange, and then I found out that any Regex with \\will create an empty result.

So is this an mgo error?

(I don't know if this is inappropriate, but I also posted this question on the mgo.users mailing list .)

+4
2

Go (\) escape ( "..." ). ( `...` ).

:

package main

import "fmt"

func main() {
    fmt.Println("^\\[^\\]*\\$")
    fmt.Println(`^\\[^\\]*\\$`)
}

:

^\[^\]*\$
^\\[^\\]*\\$

, , . , , :

err = c.Find(bson.M{"path": bson.M{"$regex": bson.RegEx{`^\\[^\\]*\\$`, ""}}}).All(&nodeList)

: http://golang.org/ref/spec#String_literals

+6

   wordOffset := anyregular expression OR text to filter

   selector:= bson.M{"title": bson.M{"$regex": wordOffset}}
0

Source: https://habr.com/ru/post/1535979/


All Articles