我想知道,是否仍然可以运行我自己的命令(或查询),我已经使用go中的“ mgo”将其构造为字符串变量。
像这样:
c := session.DB(DBNAME).C(COLLECTION)
c.RUN_COMMAND_AS_STRING("find({username:'vahid'})")
无论如何,有没有运行我自己的命令(或查询),我已经使用go中的“ mgo”将其构造为字符串变量。
您可以调用MongoDB find命令,并将查询过滤器的字符串解析为map[string]interface{}
。
例如:
db := session.DB("databaseName")
queryString := `{"username":"sergio"}`
var filter map[string]interface{}
err = json.Unmarshal([]byte(queryString), &filter)
result := bson.M{}
err = db.Run(bson.D{{"find", "collectionName"}, {"filter", filter}}, &result)
fmt.Println(result)
另外,find()
也可以使用MongoDB Aggregation Pipeline,而不是根据您的用例使用。
例如:
pipeString := `[{"$match":{"username":"sergio"}}, {"$project":{"newfield":"$username"}}]`
pipe := []bson.M{}
err = json.Unmarshal([]byte(pipeString), &pipe)
coll := session.DB("databaseName").C("collectionName")
response := []bson.M{}
err = coll.Pipe(pipe).All(&response)
fmt.Println(response)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句