gpt4 book ai didi

go - 如何在 golang 中使用 couch db 查询限制

转载 作者:IT王子 更新时间:2023-10-29 02:21:34 26 4
gpt4 key购买 nike

我正在 golang 中处理 couchdb,我需要选择限制为 1 的记录,我正在尝试使用以下芒果查询。

{"selector": {"$and": [{"memid": {"$gt": "null"}}, {"custid": {"$gt": "null"}}]},"limit" : 1}

上面的查询在浏览器上的 fauxton 中运行良好,但在 golang 中不起作用,所有结果都没有应用限制。

golang 查询:

queryString := fmt.Sprintf("{\"selector\": {\"$and\": [{\"memid\": {\"$gt\": \"%s\"}}, {\"custid\": {\"$gt\": \"%s\"}}]},\"limit\" : \"%d\"}", "null", "null",1)
queryResults, err := getQueryResultForQueryString(stub, queryString)

getQueryResultForQueryString 的函数定义:

func getQueryResultForQueryString(stub shim.ChaincodeStubInterface, queryString string) ([]byte, error) {

fmt.Printf("- getQueryResultForQueryString queryString:\n%s\n", queryString)

resultsIterator, err := stub.GetQueryResult(queryString)
if err != nil {
return nil, err
}
defer resultsIterator.Close()

// buffer is a JSON array containing QueryRecords
var buffer bytes.Buffer
buffer.WriteString("[")

bArrayMemberAlreadyWritten := false

for resultsIterator.HasNext() {
queryResponse, err := resultsIterator.Next()
if err != nil {
return nil, err
}
// Add a comma before array members, suppress it for the first array member
if bArrayMemberAlreadyWritten == true {
buffer.WriteString(",")
}
buffer.WriteString("{\"Key\":")
buffer.WriteString("\"")
buffer.WriteString(queryResponse.Key)
buffer.WriteString("\"")

buffer.WriteString(", \"Record\":")
// Record is a JSON object, so we write as-is
buffer.WriteString(string(queryResponse.Value))
buffer.WriteString("}")
bArrayMemberAlreadyWritten = true
}
buffer.WriteString("]")

fmt.Printf("- getQueryResultForQueryString queryResult:\n%s\n", buffer.String())

return buffer.Bytes(), nil
}

最佳答案

请看github.com/leesper/couchdb-golang

func (d *Database) Query(fields []string, selector string, sorts []string, limit, skip, index interface{}) ([]map[string]interface{}, error)

关于go - 如何在 golang 中使用 couch db 查询限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46715350/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com