惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
Engineering at Meta
Engineering at Meta
Forbes - Security
Forbes - Security
MongoDB | Blog
MongoDB | Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
A
About on SuperTechFans
量子位
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
雷峰网
雷峰网
腾讯CDC
P
Proofpoint News Feed
S
Schneier on Security
S
Secure Thoughts
V
Visual Studio Blog
Help Net Security
Help Net Security
The Hacker News
The Hacker News
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Privacy International News Feed
SecWiki News
SecWiki News
S
SegmentFault 最新的问题
T
Threatpost
小众软件
小众软件
MyScale Blog
MyScale Blog
F
Fortinet All Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
T
Tailwind CSS Blog
I
Intezer
C
CERT Recently Published Vulnerability Notes
U
Unit 42
V
V2EX
Cyberwarzone
Cyberwarzone
Recorded Future
Recorded Future
O
OpenAI News
Project Zero
Project Zero
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
Know Your Adversary
Know Your Adversary
C
Cybersecurity and Infrastructure Security Agency CISA
Scott Helme
Scott Helme
V2EX - 技术
V2EX - 技术
博客园 - 叶小钗
S
Securelist
A
Arctic Wolf
The Cloudflare Blog
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
博客园 - Franky

博客园 - forrestsun

问题整理 weedfs getsockopt: connection timed out 利用cubieboard设置samba打印服务器 CubieTruck上安装mjpg_streamer devexpress bandgridview使用总结(14.2) WeedFS0.6.8-引用库列表 NSQ的消息订阅发布测试 WeedFS问题收集 WeedFS依赖库 0.6.1 golang 前置补0 MongoDB-MMS使用总结 golang 格式化时间为字符串 格式化字符串为时间学习总结 mgo-后续测试(指定字段,获取id) mgo中DBRef-数据查询测试 weed-fs 基础测试 weed-fs 压力测试 weed-fs参数列表 mgo like的两种写法
mgo中DBRef-数据添加测试
forrestsun · 2014-01-25 · via 博客园 - forrestsun

2014-1-25

  在设计mongo数据库时遇到这样一个问题,日志信息表需要引用人员信息表的数据.如果是结构化数据库,基本上不用想太多的东西.由于刚接触非结构化数据库,按着书上的理解由于日志数量较多,如果采用嵌入式数据的话会产生太多的可变因素(不知道这样说是不是合理,比如说人员经常在各个部门乱跳则会造成数据经常变更 ^_^!),所以需要用到引用数据方式.今天先写了个插入数据的测试DEMO,有时间再补上个查询.

package main

import (
	"crypto/rand"
	"encoding/hex"
	"fmt"
	"labix.org/v2/mgo"
	"labix.org/v2/mgo/bson"
	"time"
)

var (
	mgoSession   *mgo.Session
	databaseName = "myDB"
	tbl_person   = "persons"
	tbl_log      = "logs"
)

type Person struct {
	Id       string
	Name     string
	Inserted time.Time
}

type Log struct {
	LogId    string
	Log      string
	LogUser  mgo.DBRef
	Inserted time.Time
}

func main() {
	session, err := mgo.Dial("localhost:27017")
	if err != nil {
		panic(err)
	}
	defer session.Close()

	session.SetMode(mgo.Monotonic, true)

	session.DB(databaseName).DropDatabase()

	c := session.DB(databaseName).C(tbl_person)
	d := session.DB(databaseName).C(tbl_log)

	tid := GenerateUUID()
	err = c.Insert(&Person{tid, "ssl", time.Now()})
	if err != nil {
		panic(err)
	}

	err = d.Insert(&Log{GenerateUUID(), "这是一个测试日志", mgo.DBRef{tbl_person, tid, databaseName}, time.Now()})
	if err != nil {
		panic(err)
	}

	result := []Person{}
	err = c.Find(bson.M{}).All(&result)
	if err != nil {
		panic(err)
	}
	fmt.Println(result)

	result1 := []Log{}
	err = d.Find(bson.M{}).All(&result1)
	fmt.Println(result1)

}

// http://www.ashishbanerjee.com/home/go/go-generate-uuid
func GenerateUUID() string {
	uuid := make([]byte, 16)
	n, err := rand.Read(uuid)
	if n != len(uuid) || err != nil {
		return ""
	}
	uuid[8] = 0x80 // variant bits see page 5
	uuid[4] = 0x40 // version 4 Pseudo Random, see page 7

	return hex.EncodeToString(uuid)
}

  上面用到了两个集合,一个人员信息,一个日志信息,主要测试是向日志表插入人员信息的引用.运行结果如下:

[ `run` | done: 2.279229602s ]
    [{0d5f77aa40146d128076f74fb0a8c926 ssl 2014-01-25 10:40:14.212 +0800 CST}]
    [{2d9250164069986580a760bca46d0e9b 这是一个测试日志 {persons 0d5f77aa40146d128076f74fb0a8c926 myDB} 2014-01-25 10:40:16.241 +0800 CST}]

  参考: