golang操作mongodb

栏目: Go · 发布时间: 7年前

内容简介:1、要安装mongodb数据库,2、要下载golang的mgo包,3、连接操作

开发十年,就只剩下这套 Java 开发体系了 >>> golang操作mongodb

1、要安装 mongodb 数据库,2、要下载golang的mgo包,3、连接操作

package main

import (
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"

	"fmt"
)

const URL = "192.168.1.83:27017" //mongodb的地址

func main() {
	session, err := mgo.Dial(URL) //连接服务器
	if err != nil {
		panic(err)
	}

	c := session.DB("ChatRoom").C("account") //选择ChatRoom库的account表

	c.Insert(map[string]interface{}{"id": 7, "name": "tongjh", "age": 25}) //增

	objid := bson.ObjectIdHex("55b97a2e16bc6197ad9cad59")

	c.RemoveId(objid) //删除

	c.UpdateId(objid, map[string]interface{}{"id": 8, "name": "aaaaa", "age": 30}) //改

	var one map[string]interface{}
	c.FindId(objid).One(&one) //查询符合条件的一行数据
	fmt.Println(one)

	var result []map[string]interface{}
	c.Find(nil).All(&result) //查询全部
	fmt.Println(result)
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Design and Analysis of Distributed Algorithms (Wiley Series on P

Design and Analysis of Distributed Algorithms (Wiley Series on P

Nicola Santoro / Wiley-Interscience / 2006-10-27 / USD 140.95

This text is based on a simple and fully reactive computational model that allows for intuitive comprehension and logical designs. The principles and techniques presented can be applied to any distrib......一起来看看 《Design and Analysis of Distributed Algorithms (Wiley Series on P》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具