golang中使用gopkg.in/mgo.v2作为字符串的自定义mongodb命令
我想知道,是否可以运行我自己的命令(或查询),我在go中使用“mgo”将其构造为字符串变量 大概是这样的:golang中使用gopkg.in/mgo.v2作为字符串的自定义mongodb命令,mongodb,go,mgo,Mongodb,Go,Mgo,我想知道,是否可以运行我自己的命令(或查询),我在go中使用“mgo”将其构造为字符串变量 大概是这样的: c := session.DB(DBNAME).C(COLLECTION) c.RUN_COMMAND_AS_STRING("find({username:'vahid'})") 以下是我喜欢使用的: func dbInsert(collection string, insert bson.M, session *mgo.Session) error { c := session
c := session.DB(DBNAME).C(COLLECTION)
c.RUN_COMMAND_AS_STRING("find({username:'vahid'})")
以下是我喜欢使用的:
func dbInsert(collection string, insert bson.M, session *mgo.Session) error {
c := session.DB(your_DB).C(collection)
err := c.Insert(insert)
return err
}
func dbUpsert(collection string, selector bson.M, update bson.M, session *mgo.Session) (*mgo.ChangeInfo, error) {
c := session.DB(your_DB).C(collection)
info, err := c.Upsert(selector, update)
return info, err
}
func dbFindOne(collection string, findBson bson.M, selectBson bson.M, session *mgo.Session) (map[string]interface{}, error) {
c := session.DB(your_DB).C(collection)
getMap := make(map[string]interface{})
err := c.Find(findBson).Select(selectBson).One(&getMap)
return getMap, err
}
func dbFindAll(collection string, findBson bson.M, selectBson bson.M, session *mgo.Session) (map[string]interface{}, error) {
c := session.DB(your_DB).C(collection)
getMap := make(map[string]interface{})
err := c.Find(findBson).Select(selectBson).All(&getMap)
return getMap, err
}
func dbUpdate(collection string, selector bson.M, update bson.M, session *mgo.Session) error {
c := session.DB(your_DB).C(collection)
setBson := bson.M{};
setBson["$set"] = update;
//
updateError := c.Update(selector, setBson)
//
return updateError
}
func dbRemoveOne(collection string, selector bson.M, session *mgo.Session) error {
c := session.DB(your_DB).C(collection)
removeError := c.Remove(selector)
return removeError
}
func dbRemoveAll(collection string, selector bson.M, session *mgo.Session) (*mgo.ChangeInfo, error) {
c := session.DB(your_DB).C(collection)
removeInfo, removeError := c.RemoveAll(selector)
return removeInfo, removeError
}
以下是查找的示例查询:
//FIND ONE:
employeeInfo, err := dbFindOne("employees", bson.M{"name": "john"}, bson.M{"salary": 1, "homeCity": 1}, mongo_session)
if err != nil {
fmt.Println("Error getting employee info: ", err)
}else{
//you can get the salary as an int:
salary := employeeInfo["salary"].(int)
//or get their homeCity as a string:
homeCity := employeeInfo["homeCity"].(string)
}
例如,在集合“employees”中查找名为“john”的员工的“salary”
代码段中的所有方法的工作方式几乎与dbFindOne()
完全相同
希望这有帮助 以下是我喜欢使用的:
func dbInsert(collection string, insert bson.M, session *mgo.Session) error {
c := session.DB(your_DB).C(collection)
err := c.Insert(insert)
return err
}
func dbUpsert(collection string, selector bson.M, update bson.M, session *mgo.Session) (*mgo.ChangeInfo, error) {
c := session.DB(your_DB).C(collection)
info, err := c.Upsert(selector, update)
return info, err
}
func dbFindOne(collection string, findBson bson.M, selectBson bson.M, session *mgo.Session) (map[string]interface{}, error) {
c := session.DB(your_DB).C(collection)
getMap := make(map[string]interface{})
err := c.Find(findBson).Select(selectBson).One(&getMap)
return getMap, err
}
func dbFindAll(collection string, findBson bson.M, selectBson bson.M, session *mgo.Session) (map[string]interface{}, error) {
c := session.DB(your_DB).C(collection)
getMap := make(map[string]interface{})
err := c.Find(findBson).Select(selectBson).All(&getMap)
return getMap, err
}
func dbUpdate(collection string, selector bson.M, update bson.M, session *mgo.Session) error {
c := session.DB(your_DB).C(collection)
setBson := bson.M{};
setBson["$set"] = update;
//
updateError := c.Update(selector, setBson)
//
return updateError
}
func dbRemoveOne(collection string, selector bson.M, session *mgo.Session) error {
c := session.DB(your_DB).C(collection)
removeError := c.Remove(selector)
return removeError
}
func dbRemoveAll(collection string, selector bson.M, session *mgo.Session) (*mgo.ChangeInfo, error) {
c := session.DB(your_DB).C(collection)
removeInfo, removeError := c.RemoveAll(selector)
return removeInfo, removeError
}
以下是查找的示例查询:
//FIND ONE:
employeeInfo, err := dbFindOne("employees", bson.M{"name": "john"}, bson.M{"salary": 1, "homeCity": 1}, mongo_session)
if err != nil {
fmt.Println("Error getting employee info: ", err)
}else{
//you can get the salary as an int:
salary := employeeInfo["salary"].(int)
//or get their homeCity as a string:
homeCity := employeeInfo["homeCity"].(string)
}
例如,在集合“employees”中查找名为“john”的员工的“salary”
代码段中的所有方法的工作方式几乎与dbFindOne()
完全相同
希望这有帮助
是否还有运行我自己的命令(或查询)的方法,我在go中使用“mgo”将其构造为字符串变量
您可以调用查询过滤器的字符串,并将其解析为映射[string]接口{}
例如:
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)
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)
或者,根据您的用例,您也可以使用
例如:
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)
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)
是否还有运行我自己的命令(或查询)的方法,我在go中使用“mgo”将其构造为字符串变量
您可以调用查询过滤器的字符串,并将其解析为映射[string]接口{}
例如:
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)
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)
或者,根据您的用例,您也可以使用
例如:
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)
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)
不,没有办法创建一个动态查询,使用
bson
并将其传递到返回集合的函数中。这太可惜了。但是tnx@HimanshuNo没有办法创建一个动态查询使用bson
并将其传递到返回集合的函数中。这太可惜了。但是tnx@HimanshuTo可以告诉任何稍后要使用它的人。首先,谢谢你的回复。但是很久以前,当我在做那个项目的时候,我不记得我该如何解决这个问题,但是如果管道中的字符串可能比你的例子复杂得多,并且在我看来是有效的,这意味着这是问题的正确答案。总的来说,我觉得这是一个正确的答案。请让我知道,如果有人尝试了它,它没有工作。给任何人谁将使用这个以后。首先,谢谢你的回复。但是很久以前,当我在做那个项目的时候,我不记得我该如何解决这个问题,但是如果管道中的字符串可能比你的例子复杂得多,并且在我看来是有效的,这意味着这是问题的正确答案。总的来说,我觉得这是一个正确的答案。请让我知道,如果有人尝试了它,它没有工作。