Skip to content

Instantly share code, notes, and snippets.

@bayucandra
Created December 16, 2019 02:47
Show Gist options
  • Save bayucandra/d61bb1ef9f5c9303833a11977c7783bb to your computer and use it in GitHub Desktop.
Save bayucandra/d61bb1ef9f5c9303833a11977c7783bb to your computer and use it in GitHub Desktop.
Mongo get duplicate field by using regular expression
package main
import (
"fmt"
"regexp"
)
func main() {
errStr := `rpc error: code = AlreadyExists desc = multiple write errors: [{write errors: [{E11000 duplicate key error ` +
`collection: mai_webmin_inventory_service.groups index: unique_name dup key: { name: "aa1234" }}]}, {<nil>}]`
r := regexp.MustCompile(`(dup key: )(.*)(}]},)`)
dupField := r.FindStringSubmatch(errStr)[2]
fmt.Println(dupField)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment