0

With BSON and Mgo(rich mongodb driver for go), how would one approach implementing materialized paths?

Materialized paths are documented here on the mongo docs. Materialized paths are designed to provide a tree-like structure for multiple levels of nested data.

With plain javascript, the command are as follows (from the documentation):

db.categories.insert( { _id: "Books", path: null } )
db.categories.insert( { _id: "Programming", path: ",Books," } )
db.categories.insert( { _id: "Databases", path: ",Books,Programming," } )
db.categories.insert( { _id: "Languages", path: ",Books,Programming," } )

Created collection 'categories' with the following entries

Books > Programming > Databases
Books > Programming > Languages

And can be queried using:

db.categories.find( { path: /,Programming,/ } )

Will find the two entries which are both decendants of programming.

How do I implement this using Mgo and BSON?

There is no such documentation in the Mgo docs. I have tried attempts such as

result := []bson.M{}
database.C("categories").Find(bson.M{"path": "/,Programming,/"}).All(&result)
fmt.Println(result)

Only to return an empty array []

Any gophers and mongo lovers to enlighten me here?

Thanks!

Community
  • 1
  • 1
William Yang
  • 759
  • 9
  • 30

1 Answers1

1

Checkout this part in mgo/bson documentation: http://godoc.org/labix.org/v2/mgo/bson#RegEx

It should probably look like this

database.C("categories").Find(bson.M{"path": &bson.RegEx{Pattern: ",Programming,", Options: "i"}}).All(&result)