Giter VIP home page Giter VIP logo

couchdb-golang's Introduction

CouchDB-Golang

Build Status GoDoc GitHub license

A Golang library for CouchDB 2.x, inspired by CouchDB-Python.

Features

  • Resource : a simple wrapper for HTTP requests and error handling
  • Server : CouchDB server instance
  • Database : CouchDB database instance
  • ViewResults : a representation of the results produced by design document views
  • ViewDefinition : a definition of view stored in a specific design document
  • Document : a representation of document object in database
  • tools/replicate : a command-line tool for replicating
func (d *Database) Query(fields []string, selector string, sorts []string, limit, skip, index interface{}) ([]map[string]interface{}, error)

You can query documents using a conditional selector statement in Golang. It will converts to the corresponding JSON query string.

  • selector: A filter string declaring which documents to return, formatted as a Golang statement.
  • fields: Specifying which fields to be returned, if passing nil the entire is returned, no automatic inclusion of _id or other metadata fields.
  • sorts: How to order the documents returned, formatted as ["desc(fieldName1)", "desc(fieldName2)"] or ["fieldNameA", "fieldNameB"] of which "asc" is used by default, passing nil to disable ordering.
  • limit: Maximum number of results returned, passing nil to use default value(25).
  • skip: Skip the first 'n' results, where 'n' is the number specified, passing nil for no-skip.
  • index: Instruct a query to use a specific index, specified either as "<design_document>" or ["<design_document>", "<index_name>"], passing nil to use primary index(_all_docs) by default.

For example:

docsQuery, err := movieDB.Query(nil, `year == 1989 && (director == "Ademir Kenovic" || director == "Dezs Garas")`, nil, nil, nil, nil)

equals to:

docsRaw, err := movieDB.QueryJSON(`
{
  "selector": {
    "year": 1989,
    "$or": [
      { "director": "Ademir Kenovic" },
      { "director": "Dezs Garas" }
    ]
  }
}`)

Inner functions for selector syntax

  • nor(condexprs...) matches if none of the conditions in condexprs match($nor). For example: nor(year == 1990, year == 1989, year == 1997) returns all documents whose year field not in 1989, 1990 and 1997.

  • all(field, array) matches an array value if it contains all the elements of the argument array($all). For example: all(genre, []string{"Comedy", "Short"} returns all documents whose genre field contains "Comedy" and "Short".

  • any(field, condexpr) matches an array field with at least one element meets the specified condition($elemMatch). For example: any(genre, genre == "Short" || genre == "Horror") returns all documents whose genre field contains "Short" or "Horror" or both.

  • exists(field, boolean) checks whether the field exists or not, regardless of its value($exists). For example: exists(director, false) returns all documents who does not have a director field.

  • typeof(field, type) checks the document field's type, valid types are "null", "boolean", "number", "string", "array", "object"($type). For example: typeof(genre, "array") returns all documents whose genre field is of array type.

  • in(field, array) the field must exist in the array provided($in). For example: in(director, []string{"Mike Portnoy", "Vitali Kanevsky"}) returns all documents whose director field is "Mike Portnoy" or "Vitali Kanevsky".

  • nin(field, array) the document field must not exist in the array provided($nin). For example: nin(year, []int{1990, 1992, 1998}) returns all documents whose year field is not in 1990, 1992 or 1998.

  • size(field, int) matches the length of an array field in a document($size). For example: size(genre, 2) returns all documents whose genre field is of length 2.

  • mod(field, divisor, remainder) matches documents where field % divisor == remainder($mod). For example: mod(year, 2, 1) returns all documents whose year field is an odd number.

  • regex(field, regexstr) a regular expression pattern to match against the document field. For example: regex(title, "^A") returns all documents whose title is begin with an "A".

Inner functions for sort syntax

asc(field) sorts the field in ascending order, this is the default option while desc(field) sorts the field in descending order.

Requirements

  • Golang 1.7.x and above

Installation

go get -u -v github.com/leesper/couchdb-golang

Authors and acknowledgment

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update unit tests as appropriate.

couchdb-golang's People

Contributors

jcantonio avatar leesper avatar paduraru avatar philippwinter avatar serkansipahi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.