【问题标题】:Custom mongodb command in golang using gopkg.in/mgo.v2 as string使用 gopkg.in/mgo.v2 作为字符串的 golang 中的自定义 mongodb 命令
【发布时间】:2023-03-27 14:00:01
【问题描述】:

我想知道,是否有运行我自己的命令(或查询),我在 go 中使用“mgo”构造为字符串变量。

类似这样的:

c := session.DB(DBNAME).C(COLLECTION)
c.RUN_COMMAND_AS_STRING("find({username:'vahid'})")

【问题讨论】:

  • 不,没有办法这样做 要创建动态查询,请使用 bson 并将其传递给返回集合的函数。
  • 真可惜。但是 tnx @Himanshu

标签: mongodb go mgo


【解决方案1】:

是否有运行我自己的命令(或查询),我在 go 中使用“mgo”构造为字符串变量。

您可以调用MongoDB find command,并将查询过滤器字符串解析为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)

或者,根据您的用例,您也可以使用MongoDB Aggregation Pipeline,而不是使用find()

例如:

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)

【讨论】:

  • 致以后要使用它的任何人。首先,感谢您的回复。但是很久以前,当我从事那个项目时,目前我不记得我该如何解决这个问题,但是如果 pipeString 中的字符串可能比您的示例复杂得多并且它可以在我认为这意味着这是该问题的正确答案。总的来说,我觉得它是一个正确的答案。请让我知道是否有人尝试过但没有成功。
【解决方案2】:

这是我喜欢使用的:

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 的示例查询:

//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”。

sn-p 中的所有方法的工作方式几乎与dbFindOne() 完全相同。

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    • 1970-01-01
    • 2011-08-16
    • 2021-09-11
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    相关资源
    最近更新 更多