如何在mgo中搜索嵌入对象的键?
how to search a key of embed object in mgo?
query:=bson.M{"relationship.from": bson.RegEx{"\d+8.*", ""}}
c.Find(query).All(&users)
上面的代码 returns 空集,但集合看起来像:
{"name" : "test","phone": "13xxxx" "relationship":{"from":1982, "to": 1998}}
{"name" : "test2", "phone": "15xxx","relationship":{"from":1981, "to": 1999}}
如果搜索 phone 键就可以了,但在 mongodb 控制台中失败了:
db.users.find({"relationship.from": /\d+8\d*/})
没有匹配的结果??
正则表达式只能匹配字符串,但这些文档中的字段值为数字
要匹配这些,请查看比较查询运算符:
http://docs.mongodb.org/manual/reference/operator/query-comparison/
query:=bson.M{"relationship.from": bson.RegEx{"\d+8.*", ""}}
c.Find(query).All(&users)
上面的代码 returns 空集,但集合看起来像:
{"name" : "test","phone": "13xxxx" "relationship":{"from":1982, "to": 1998}}
{"name" : "test2", "phone": "15xxx","relationship":{"from":1981, "to": 1999}}
如果搜索 phone 键就可以了,但在 mongodb 控制台中失败了:
db.users.find({"relationship.from": /\d+8\d*/})
没有匹配的结果??
正则表达式只能匹配字符串,但这些文档中的字段值为数字
要匹配这些,请查看比较查询运算符:
http://docs.mongodb.org/manual/reference/operator/query-comparison/