如何使用mgo在golang中编写mongodb搜索

时间:2016-05-19 12:06:17

标签: go mgo

这是我的JSON文件:

int getState(const int stateCode=0){
  int outState;
  //state 0
if(stateCode == 0 &&(fabs(verticalSpeed) < 2 || fabs(relativeAltitude) < 5)&&initializing){
  outState = 0;

  //true should change later to see is launch botton is on
} else
if(missionReady && !initializing && stateCode == 0){
    outState = 1;
}else if(verticalSpeed > 3&&stateCode == 1){
    outState = 2;
} 
//else 
//if(VerticalSpeed < 3 && stateCode == 2){
//  stateCode = 2;
//  //apogee but no code in CDR
//} 
else if(verticalSpeed < 2 &&stateCode == 2){
      outState = 3;
} else if((fabs(relativeAltitude - 450)< 10 || relativeAltitude<440 ) &&stateCode == 3){
        outState = 4;
}else
//true should be replaced with seperation photocell is bright
if(true && stateCode == 4){
  outState = 5;
}else if(relativeAltitude < 3 && fabs(verticalSpeed) < .7 && stateCode == 5  ){
  outState == 6;
  //activate Buzzer stop telemetry
}


return outState;
}

这是我尝试的golang代码,但没有按预期工作。

[{
    "name": "chetan",
    "age": 23,
    "hobby": ["cricket", "football"]
}, {
    "name": "raj",
    "age": 24,
    "hobby": ["cricket", "golf"]
}]

它给出错误:

  

$业余爱好退出状态1

1 个答案:

答案 0 :(得分:2)

$search我假设您正在尝试使用文本索引/搜索,但在您的情况下,这将无效。文本索引不支持partials。您仍然可以使用正则表达式来查找这些文档,但是在性能方面它可能不是一个明智的选择,除非您可以使用索引 - 在您的情况下不会发生。

仍然,你可以实现你想要的目标:

id := "ket"
regex := bson.M{"$regex": bson.RegEx{Pattern: id}}
err = c.Find(bson.M{"hobby": regex}).All(&result)