使用Go插入和查询MongoDB数据
这就是代码, 如果我运行MongoShell,我会得到正确的结果:使用Go插入和查询MongoDB数据,mongodb,go,Mongodb,Go,这就是代码, 如果我运行MongoShell,我会得到正确的结果: package main import ( "fmt" "log" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" ) type Customer struct { Id bson.ObjectId `bson:"_id,omitempty"` id int `bson:
package main
import (
"fmt"
"log"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type Customer struct {
Id bson.ObjectId `bson:"_id,omitempty"`
id int `bson:"id,"`
firstName string `bson:"firstName"`
surname string `bson:"surname"`
gender string `bson:"gender"`
address1 string `bson:"address1"`
address2 string `bson:"address2"`
city string `bson:"city"`
state_region string `bson:"state_region"`
county_province string `bson:"county_province"`
postalCode string `bson:"postalCode"`
country string `bson:"country"`
acct_bal float64 `bson:"acct_bal"`
status string `bson:"status"`
}
func main() {
uri := "localhost:27017"
// connect to mongodb
session, err := mgo.Dial(uri)
if err != nil {
log.Fatal("Couldn't connect to db.", err)
}
defer session.Close()
// collection
c := session.DB("mydb").C("customers")
// query one
result := Customer{}
err = c.Find(bson.M{"status": "B"}).One(&result)
if err != nil {
log.Fatal("Couldn't find him.", err)
}
fmt.Println("One Result: ", result)
}
}
但运行时的Go文件给了我以下信息:
{
"_id" : ObjectId("528cb19def5c88795f00000a"),
"id" : "00000011",
"firstName" : "Gerardo",
"surname" : "Guilfoos",
"gender" : "M",
"address1" : "854 Cheerful Breeze Way",
"address2" : "",
"city" : "Tavaux",
"state_region" : "Franche-Comté",
"county_province" : "Jura",
"postalCode" : "39501 CEDEX",
"country" : "FR",
"acct_balance" : 172.87,
"status" : "B"
我遵循Udemy的课程,他们提供了数据结构。不幸的是,他们提供的示例都是用PHP编写的,所以我不得不想出一种方法来转换所有代码,这很奇怪
另请注意:
如何将与该结构匹配的数据插入mongoDB集合?
我试过了,但失败了
One Result: {ObjectIdHex("528cb19def5c88795f00000a") 0 0 }
字段名
err = c.Insert(&Customer{"id": 1, "firstName": "Joe", "surname": "Hat", "gender": "M", "address1": "46 Pine Road", "address2": "Apartment 1613", "city": "Scarborough", "state_region": "G.T.A", "county_provine": "Ontario", "postalCode": "M1L 1N1", "country": "Canada", "acct_bal": 8.90, "status": "AAA",})
BSON编码器。其他序列化程序(如encoding/json和encoding/gob)也只能处理导出的字段。id字段是字符串,而不是您在应用程序中定义的整数struct@user3513407是否需要在数据库中同时存储id和_id?如果整数id是唯一的,您可以将其用作_id。对于如何解决插入问题,您有什么建议吗?理想情况下,我想要一个解决方案,你可以分开的东西。数据:=stuff to insert err=c.Insertdata数据行是显示我所有错误的地方。提前感谢您的帮助。我只是尝试匹配课程讲师提供的数据结构。数据:=&Customer{ID:10,FirstName:John};错误:=c。Insertdata是否解决此问题?你可以更换;使用换行符.c:=session.DBmydb.Ccustomers数据:=&Customer{ID:10,名字:约翰,姓氏:麦当劳,性别:M,地址1:1登月,地址2:rockbottom,城市:纽约,州/地区:纽约}err=c.Insertdata if err!=nil{log.Fatalerr}//查询一个结果:=Customer{}err=c.Findbson.M{FirstName:John}。如果出现错误,则查询一个结果(&R)=nil{log.fatalcould找不到他,err}fmt.PrintlnOne Result:,Result Results::struct literal中未知的客户字段“姓氏”
type Customer struct {
Id bson.ObjectId `bson:"_id,omitempty"`
ID int `bson:"id"`
FirstName string `bson:"firstName"`
Surname string `bson:"surname"`
Gender string `bson:"gender"`
Address1 string `bson:"address1"`
Address2 string `bson:"address2"`
City string `bson:"city"`
State_region string `bson:"state_region"`
County_province string `bson:"county_province"`
PostalCode string `bson:"postalCode"`
Country string `bson:"country"`
Acct_bal float64 `bson:"acct_bal"`
Status string `bson:"status"`
}