我是 mgo 的新手,需要一些帮助: 我可以成功连接并打印出数据库名称、集合名称和项目编号是集合,但不知道如何打印其中的内容并写回。 mgo 中与以下 mongodb shell 命令等效的是什么?
- db.coll.find()
- document=({"user_id" : "xxx","password" :"xxx"....});
- db.coll.insert(document)
//////////////////////////////////////////////////////////////////
package main
import (
"fmt"
"time"
"gopkg.in/mgo.v2"
)
//const MongoDb details
const (
hosts = "mongodb.xxx:27017"
database = "myinfo"
username = "xxxxx"
password = "start123"
collection = "userdetails"
)
func main() {
info := &mgo.DialInfo{
Addrs: []string{hosts},
Timeout: 60 * time.Second,
Database: database,
Username: username,
Password: password,
}
session, err1 := mgo.DialWithInfo(info)
if err1 != nil {
panic(err1)
}
col := session.DB(database).C(collection)
datab := session.DB(database)
count, err2 := col.Count()
if err2 != nil {
panic(err2)
}
fmt.Println("Database Name:", datab.Name)
fmt.Println("Collection FullName:", col.FullName)
fmt.Println(fmt.Sprintf("Documents count: %d", count))
}
这是一个有效的版本:
package main
import (
"fmt"
"time"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
//const MongoDb details
const (
hosts = "xxx:27017"
database = "myinfo"
username = "xxxx"
password = "start123"
collection = "userdetails2"
)
type UserDetails struct {
_id bson.ObjectId `bson:"_id,omitempty"`
name string
phone string
}
func main() {
info := &mgo.DialInfo{
Addrs: []string{hosts},
Timeout: 60 * time.Second,
Database: database,
Username: username,
Password: password,
}
session, err1 := mgo.DialWithInfo(info)
if err1 != nil {
panic(err1)
}
col := session.DB(database).C(collection)
datab := session.DB(database)
count, err2 := col.Count()
if err2 != nil {
panic(err2)
}
fmt.Println("Database Name:", datab.Name)
fmt.Println("Collection FullName:", col.FullName)
fmt.Println(fmt.Sprintf("Documents count: %d", count))
var userDetail []bson.M
_ = col.Find(nil).All(&userDetail)
for _, v := range userDetail {
fmt.Println(v)
}
}
最佳答案
试试这个:
import (
"gopkg.in/mgo.v2/bson"
)
type UserDetails struct {
Id bson.ObjectId `bson:"_id,omitempty"`
UserId string `bson:"user_id"`
Password string `bson:"password"`
}
userDetails := []UserDetails{}
query := bson.M{
"user_id": "xxx",
"password" :"xxx"
}
err := col.Find(query).All(&userDetails)
if err != nil {
return
}
for _, userDetail := range userDetails {
fmt.Println(userDetail)
}
我还没有测试这段代码。这只是一个例子。
关于mongodb - mgo mongodb 读/写示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50423041/
//1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json
我对图像处理完全陌生。我对JPEG内部是什么以及它是如何工作一无所知。我想知道,是否可以在某处找到执行以下简单操作的ruby代码:打开jpeg文件。遍历每个像素并将其颜色设置为fx绿色。将结果写入另一个文件。我对如何使用ruby-vips库实现这一点特别感兴趣https://github.com/ender672/ruby-vips我的目标-学习如何使用ruby-vips执行基本的图像处理操作(Gamma校正、亮度、色调……)任何指向比“helloworld”更复杂的工作示例的链接——比如ruby-vips的github页面上的链接,我们将不胜感激!如果有ruby-
我已经有很多两个值数组,例如下面的例子ary=[[1,2],[2,3],[1,3],[4,5],[5,6],[4,7],[7,8],[4,8]]我想把它们分组到[1,2,3],[4,5],[5,6],[4,7,8]因为意思是1和2有关系,2和3有关系,1和3有关系,所以1,2,3都有关系我如何通过ruby库或任何算法来做到这一点? 最佳答案 这是基本Bron–Kerboschalgorithm的Ruby实现:classGraphdefinitialize(edges)@edges=edgesenddeffind_maximum_
很高兴看到google代码:google-api-ruby-client项目,因为这对我来说意味着Ruby人员可以使用GoogleAPI-s来完善代码。虽然我现在很困惑,因为给出的唯一示例使用Buzz,并且根据我的实验,Google翻译(v2)api的行为必须与google-api-ruby-client中的Buzz完全不同。.我对“Explorer”演示示例很感兴趣——但据我所知,它并不是一个探索器。它所做的只是调用一个Buzz服务,然后浏览它已经知道的关于Buzz服务的事情。对我来说,Explorer应该让您“发现”所公开的服务和方法/功能,而不一定已经知道它们。我很想听听使用这个
在他们的网站上找不到任何内容。我主要只是想看看哪个值得一试(当然是RIA)。谢谢 最佳答案 SproutCoredemos 关于ruby-是否有SproutCore或Cappuccino的现场演示/示例应用程序,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/1419788/
我对自动测试的工作方式的印象(基于cucumbergithubwiki和其他在线内容)是它应该重新运行红色示例,直到它们通过。我的问题是它会重新运行规范文件中找到失败示例的所有示例,包括通过的示例。我不想浪费时间在修复失败示例的同时重新运行通过的示例。是否可以配置自动测试以便仅运行失败的示例? 最佳答案 您需要rspec-retrygem。以下是文档中有关如何实现它的一些示例:将它应用到覆盖整个测试套件的configureblock中...RSpec.configuredo|config|config.verbose_retry=t
我找不到任何使用Rack::Session::Cookie的简单示例,并且希望能够将信息存储在cookie中,并在以后的请求中访问它并让它过期.这些是我能找到的唯一示例:HowdoIset/getsessionvarsinaRackapp?http://rack.rubyforge.org/doc/classes/Rack/Session/Cookie.html这是我得到的:useRack::Session::Cookie,:key=>'rack.session',:domain=>'foo.com',:path=>'/',:expire_after=>2592000,:secret=
我正在构建一个部署在heroku上的rails5应用程序。我想用AWScongnito来实现单点登录,但是没有足够的例子来实现。我正在使用devise进行身份验证。现在我的目标是让我的所有用户都使用AWScognito并通过我的Rails应用程序对他们进行身份验证。这是我在AWScongnitowithrails上找到的唯一资源,我正在寻找一些示例应用程序或指向工具或rubyAPI文档的链接来实现此目的。请帮助。UpdateOnbasisOfBalaAnswerrequire'aws-sdk'ENV['AWS_ACCESS_KEY_ID']='XXXXXXXXXXXXXXXXX'E
在一堆rspecrails单元规范中,我做了类似的事情:describeFoodo[:bar,:baz].eachdo|a|it"shouldhavemany#{a}"doFoo.shouldhave_many(a)endendend为了更简洁的代码,我宁愿这样做:describeFoodospec_has_manyFoo,:bar,:bazend那么我该如何编写像spec_has_many()这样的辅助方法来像rspec的it()方法那样插入DSL代码呢?如果它是一个普通的实例方法,我会做类似的事情:defspec_has_many(model,*args)args.eachdo|a
我需要使用正则表达式在Ruby中匹配字符串中的表情符号。我已经尝试了几个unicode序列,但似乎没有一个能完全胜任。我也不确定表情符号的开始和结束范围在哪里。 最佳答案 这个正则表达式匹配所有845个表情符号,取自Emojiunicodecharactersforuseontheweb:[\u{203C}\u{2049}\u{20E3}\u{2122}\u{2139}\u{2194}-\u{2199}\u{21A9}-\u{21AA}\u{231A}-\u{231B}\u{23E9}-\u{23EC}\u{23F0}\u{23F3