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操作的最简单的例子.
随机推荐
- 【转】Android 快捷方式的创建
http://blog.csdn.net/lenmoyouzi/article/details/16939977 一.在日常开发中,我们经常会遇到这样的需求就是网桌面添加快捷方式:常见的快捷方式有两种 ...
- android开发:@SuppressLint( NewApi )
这个是android带的lint工具提示的,lint官方的说法是 Improving Your Code with lint,应该是帮助提升代码的 ,如果不想用的话,可以右键点工程,然后在androi ...
- 1032 - Intersecting Dates
A research group is developing a computer program that will fetch historical stock market quotes fro ...
- [Webpack 2] Maintain sane file sizes with webpack code splitting
As a Single Page Application grows in size, the size of the payload can become a real problem for pe ...
- [rxjs] Throttled Buffering in RxJS (debounce)
Capturing every event can get chatty. Batching events with a throttled buffer in RxJS lets you captu ...
- 【Java基础】Jar包结构结构分析和操作具体解释
作者:郭嘉 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.com/AllenWell 一 ...
- 自定义String类,并且实现在STL容器中添加自定义的类型
13.44 编写标准库string类的简化版本,命名String.你的类应该至少有一个默认构造函数和一个接受C风格字符串指针参数的构造函数.使用allocator为你的String类分配所需内存. 1 ...
- MapReduce链接作业
对于简单的分析程序,我们只需一个MapReduce就能搞定,然而对于比较复杂的分析程序,我们可能需要多个Job或者多个Map或者Reduce进行计算.下面我们来说说多个Job或者多个MapReduce ...
- oracle设定用户密码使用时间
强制用户定期更换密码,要怎么设置? 假设密码用10天之后必须修改,宽限期为2天: 把电脑时间往后调十天,然后登录: 系统提示用户密码两天内失效,这时把电脑系统再往后调两天,然后登录: 系统提示密码已经 ...
- 《转载》CSS中的三种样式来源:创作人员、读者和用户代理
CSS中的样式一共有三种来源:创作人员.读者和用户代理,来源的不同会影响到样式的层叠方式,很多第一次学习CSS的朋友,对这三种来源可能会存在一些困惑,下面我写一下自己的理解,若有错误的地方还请指正. ...