OnSQL

command module
v0.0.0-...-ed53f6e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2024 License: MIT Imports: 2 Imported by: 0

README

OnSQL

用SQL查询MongoDB

Test

快速上手

package main

import "fmt"
import "github.com/boyxp/OnSQL/MongoDB"

func main() {
	//注册MongoDB数据库信息(tag标签、数据库名称、dsn)
	MongoDB.Register("demo", "test", "mongodb://localhost:27017")

	//指定tag标签和集合名,用tag方便数据库改名
	shop := MongoDB.Model{"demo.shop"}

	//插入记录
	shop.Insert(map[string]interface{}{"name":"可口可乐","price":100,"detail":"...","category":"饮料"})
	shop.Insert(map[string]interface{}{"name":"小红帽","price":200,"detail":"...","category":"服装"})
	shop.Insert(map[string]interface{}{"name":"雪碧","price":300,"category":"饮料"})

	//读取记录
	list := shop.Field("name,price").
				Where("price", ">", 100).
				Order("price", "desc").
				Page(1).Limit(10).Select()
	fmt.Println(list)

	//聚合查询
	aggs := shop.Field("category,count(*) total,sum(price) as sum_price,avg(price) as avg_price,min(price) as min_price,max(price) as max_price").
			Group("category").Select()
	for k, v := range aggs {
		fmt.Println(k, v)
	}
}

更多示例见单元测试

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL