golang-mongodb范例
package main import (
"log" "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
) type Address struct {
Address string
}
type Location struct {
Longitude float64
Latitude float64
} type Person struct {
Id bson.ObjectId `bson:"_id"`
Name string
Age_Int int
Address []Address
Location Location
} func main() {
log.SetFlags(log.Flags() | log.Lshortfile)
//连接
session, err := mgo.Dial("127.0.0.1:27017")
if err != nil {
log.Println(err)
return
}
//设置模式
session.SetMode(mgo.Monotonic, true)
//获取文档集
collection := session.DB("test").C("person")
// 创建索引
index := mgo.Index{
Key: []string{"name"}, // 索引字段, 默认升序,若需降序在字段前加-
Unique: true, // 唯一索引 同mysql唯一索引
DropDups: true, // 索引重复替换旧文档,Unique为true时失效
Background: true, // 后台创建索引
}
if err := collection.EnsureIndex(index); err != nil {
log.Println(err)
return
}
if err := collection.EnsureIndexKey("$2dsphere:location"); err != nil { // 创建一个范围索引
log.Println(err)
return
}
//添加记录
person := Person{
Id: bson.NewObjectId(),
Name: "逍遥",
Age_Int: ,
Address: []Address{
Address{
Address: "仙灵岛",
},
},
Location: Location{
Longitude: ,
Latitude: ,
},
}
if err := collection.Insert(person); err != nil {
log.Println(err)
return
}
//查找记录
newPerson := &Person{}
if err := collection.Find(bson.M{"age_int": }).One(newPerson); err != nil {
log.Println(err)
return
}
//修改记录
if err := collection.Update(bson.M{"age_int": }, bson.M{"$set": bson.M{"age_int": }}); err != nil {
log.Println(err)
return
}
//删除记录
//if err := collection.Remove(bson.M{"age_int": 26}); err != nil {
// log.Println(err)
// return
//}
//位置搜索
selector := bson.M{
"location": bson.M{
"$near": bson.M{
"$geometry": bson.M{
"type": "Point",
"coordinates": []float64{, },
},
"$maxDistance": ,
//"$minDistance": 0,
},
},
}
if err := collection.Find(selector).One(newPerson); err != nil {
log.Println(err)
return
}
//
session.Close()
}
golang-mongodb范例的更多相关文章
- Golang+Mongodb
Golang+Mongodb打造你的第一个站点 很多人推荐MEAN来开发站点.MEAN就是M:mongodb,E:expressjs.A:angular最后的N:nodejs. 但是如果你亲身的体会到 ...
- golang mongodb (mgo)插入或读取文档的字段值为空(nil)问题解决
问题描述 当使用mgo操作mongodb时,遇到数据插入正常,但是在数据库查看时只能看到objectid被插入了:读取的时候,可以查询到记录,但是读入对象时,却所有的值均为0或者空的情况. mongo ...
- 使用Golang+Mongodb打造你的第一个站点
很多人推荐MEAN来开发站点.MEAN就是M:mongodb,E:expressjs.A:angular最后的N:nodejs. 但是如果你亲身的体会到了js的嵌套回调的话你就会想换换别的办法了.虽然 ...
- golang mongodb查找find demo
使用gopkg.in/mgo.v2库操作,插入操作主要使用mongodb中Collection对象的Find方法,函数原型: func (c *Collection) Find(query inter ...
- golang mongodb 驱动二次封装
mongodb 官方的go驱动包 go.mongodb.org/mongo-driver 使用起来比较繁琐,最近对其进行了二次封装 github地址:https://github.com/w3liu/ ...
- NoSQL学习1
MongoDB使用C++语言编写的一个基于分布式的文件存储的开源数据库.可以在承受高负载的情况下,保证服务器的性能. MongoDB将数据存储成为一个文档,数据结构有键值对组成.类似于JSON,字段值 ...
- 处理范例代码Webapi中的Mongodb的Bson中ObjectId反序列化异常
微软代码范例中的一个Bug 处理Mongodb的Bson中ObjectId反序列化异常 https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/f ...
- golang学习之mgo操作mongodb
mgo是mongodb的golang驱动,测试代码: // mgotest project main.go package main import ( "fmt" "ti ...
- Golang使用MongoDB通用操作
MongoDB是Nosql中常用的一种数据库,今天笔者就简单总结一下Golang如何使用这些通用的供能的,不喜勿喷... 研究的事例结构如下: type LikeBest struct { Autho ...
- 『Golang』MongoDB在Golang中的使用(mgo包)
有关在Golang中使用mho进行MongoDB操作的最简单的例子.
随机推荐
- web配置nagios工具
Nagios是一款开源的免费网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等.在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员, ...
- python学习笔记(六)文件夹遍历,异常处理
python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...
- Tomcat配置gzip压缩
HTTP 压缩能够大大提高浏览站点的速度,它的原理是,在client请求网 页后,从server端将网页文件压缩,再下载到client,由client的浏览器负责解 压缩并浏览.相对于普通的浏览过程H ...
- 自主创建tcpdump/wireshark pcap文件
pcap文件格式是bpf保存原始数据包的格式,很多软件都在使用,比如tcpdump.wireshark等等,了解pcap格式可以加深对原始数据包的了解,自己也可以手工构造任意的数据包进行测试. p ...
- android开发之自定义AutoCompleteTextView
AutoCompleteTextView,很多人都用过,有些情况下使用Google提供的ArrayAdapter作为适配器就可以完成需求,但是在实际开发中,我们经常需要开发自定义适配器来完成开发工作. ...
- xhEditor与Java结合使用
xhEditor是一个轻量级的html编辑器,使用它可以非常方便的编辑图文内容,然而官方文档中只有php的演示,没有Java版的,最近两天参考网上各种各样的文档,琢磨了一下用法,现已可以正常运行,现在 ...
- [视频转换] C#VideoConvert视频转换帮助类 (转载)
点击下载 VideoConvert.zip 主要功能如下 .获取文件的名字 .获取文件扩展名 .获取文件类型 .视频格式转为Flv .生成Flv视频的缩略图 .转换文件并保存在指定文件夹下 .转换文件 ...
- A题笔记(8)
No. 2878 No. 2559 都是输入两个数,让你来判断是否符合要求的 特别注意 2878 , 题目中要求 1<=a,b<=2^64-1(2的64次方-1)= 18446744073 ...
- 345. Reverse Vowels of a String(C++)
345. Reverse Vowels of a String Write a function that takes a string as input and reverse only the v ...
- Mindjet MindManager 2012 从模板创建出现“Runtime Error pure virtual function call” 解决方法
我的Mindjet MindManager 2012 Pro也就是MindManager10 在应用模板之后总会显示 Microsoft Visual C++ Runtime Library Runt ...