Mgo(用于Go的MongoDB驱动程序)是否支持材料化路径?

huangapple go评论98阅读模式
英文:

Mgo (mongo for go) support for materialized paths?

问题

使用BSON和Mgo(用于Go的强大MongoDB驱动程序),如何实现材料化路径(Materialized paths)?

材料化路径在MongoDB文档中有详细说明。材料化路径旨在为多层嵌套数据提供类似树状结构的表示。

使用纯JavaScript,命令如下(来自文档1):

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," } )

创建了名为'categories'的集合,并插入了以下条目:

Books > Programming > Databases
Books > Programming > Languages

可以使用以下查询语句进行查询:

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

将找到两个条目,它们都是Programming的后代。

如何使用MgoBSON来实现这个功能?

在Mgo文档中没有这样的说明。我尝试了以下方法:

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

但只返回一个空数组[]

有没有Go语言和MongoDB方面的专家可以给我指点一下?

谢谢!

英文:

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!

答案1

得分: 1

请看一下 mgo/bson 文档中的这部分内容:http://godoc.org/labix.org/v2/mgo/bson#RegEx

它应该是这样的:

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

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)

huangapple
  • 本文由 发表于 2015年9月22日 06:11:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/32705317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定