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

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
NISL@THU
NISL@THU
S
Securelist
O
OpenAI News
S
Security Affairs
Cyberwarzone
Cyberwarzone
T
Threatpost
Simon Willison's Weblog
Simon Willison's Weblog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 最新话题
C
Cisco Blogs
PCI Perspectives
PCI Perspectives
SecWiki News
SecWiki News
S
Secure Thoughts
GbyAI
GbyAI
I
Intezer
AWS News Blog
AWS News Blog
F
Fortinet All Blogs
I
InfoQ
阮一峰的网络日志
阮一峰的网络日志
Google Online Security Blog
Google Online Security Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
A
About on SuperTechFans
S
Schneier on Security
P
Proofpoint News Feed
雷峰网
雷峰网
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
小众软件
小众软件
H
Heimdal Security Blog
Microsoft Security Blog
Microsoft Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs
V
V2EX
L
Lohrmann on Cybersecurity
Security Latest
Security Latest
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
H
Hacker News: Front Page
Cisco Talos Blog
Cisco Talos Blog
Webroot Blog
Webroot Blog
T
Tenable Blog
MyScale Blog
MyScale Blog
博客园 - 司徒正美
S
SegmentFault 最新的问题
Y
Y Combinator Blog
腾讯CDC
Hacker News: Ask HN
Hacker News: Ask HN
M
MIT News - Artificial intelligence
G
GRAHAM CLULEY

博客园 - 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中DBRef-数据查询测试 mgo中DBRef-数据添加测试 weed-fs 基础测试 weed-fs 压力测试 weed-fs参数列表 mgo like的两种写法
mgo-后续测试(指定字段,获取id)
forrestsun · 2014-01-25 · via 博客园 - forrestsun

测试完mgo中的DBRef后,想接着测试指定字段的显示,才发现原来采用框架编码,很多问题被隐藏了起来:

1.显示指定字段:

  之前在使用mgo时一直是查询全部字段,在mongo终端环境写为如下格式:

> db.logs.find({},{"log":1})
{ "_id" : "3a06384a40a8e587806f194c6c80253e", "log" : "这是一个测试日志" }
{ "_id" : "36bb718040a4362b8035ebd822125dba", "log" : "这是一个测试日志" }

为了保证只显示log内容,需要去掉id显示,则写成这样

> db.logs.find({},{"_id":0,"log":1})
{ "log" : "这是一个测试日志" }
{ "log" : "这是一个测试日志" }

 在golang中的mgo写法却需要借助mgo中的select()方法实现,代码如下:

err = d.Find(bson.M{}).Select(bson.M{"log": 1}).All(&result1)

结果如下:

[ `run` | done: 342.828631ms ]
[{b5e57fed409eab8e804e17088b1fdaae 这是一个测试日志 { <nil> } 0001-01-01 00:00:00 +0000 UTC} 
{261e7fe9402c6842807092e7f0df61ce 这是一个测试日志 { <nil> } 0001-01-01 00:00:00 +0000 UTC}

去掉id显示:

err = d.Find(bson.M{}).Select(bson.M{"log": 1, "_id": 0}).All(&result1)

 结果

{ 这是一个测试日志 { <nil> } 0001-01-01 00:00:00 +0000 UTC} 
{ 这是一个测试日志 { <nil> } 0001-01-01 00:00:00 +0000 UTC}

嗯.....如果想只显示一个字段,我现在的办法是进行循环输出.

	for i := 0; i < len(result1); i++ {
		fmt.Println(result1[i].Log)
	}

  参考:mgo如何返回部分field?

2.查询ID的值:

在mongo终端很简单:

> db.logs.find({},{"_id":1})
{ "_id" : "3a06384a40a8e587806f194c6c80253e" }
{ "_id" : "36bb718040a4362b8035ebd822125dba" }

在mgo下却查不出来?问题在struct中

//错误写法
type Log struct {
	LogId    string
	Log      string
	LogUser  mgo.DBRef
	Inserted time.Time
}

//正确写法
type Log struct {
	LogId    string `bson:"_id"`
	Log      string
	LogUser  mgo.DBRef
	Inserted time.Time
}

 再查询ID没有问题了:),注意查的时间是查"_id"而不是logid

err = d.Find(bson.M{}).Select(bson.M{"_id": 1}).All(&result1)

  结果:

{32197b67400e229f8017fd8258c2f700  { <nil> } 0001-01-01 00:00:00 +0000 UTC} 
{e7dda7b34052584280ef55f49b16eb2c { <nil> } 0001-01-01 00:00:00 +0000 UTC}

  参考:mgo 如何根据指定的 _id 查询到结果啊?