mongo-特性

mongo-特性

  • Install
  • BSON
  • CRUD
  • Capped Collection(固定集合)
  • Aggregation
  • indexing
  • GridFS
  • Replication
  • Sharding
  • mongo shell
<div class="post-text" itemprop="text"> <p>I am trying to parse the results of a MongoDB query from Go. I have document(s) that output from my Database as a result of:</p> <pre><code>db.getCollection('People').find({}) { "_id" : ObjectId("5730fd75113c8b08703b5974"), "firstName" : "George", "lastName" : "FakeLastName" } { "_id" : ObjectId("5730fd75113c8b08703b5975"), "firstName" : "John", "lastName" : "Doe" } { "_id" : ObjectId("5730fd75113c8b08703b5976"), "firstName" : "Jane", "lastName" : "Doe" } </code></pre> <p>Here is the Go code that I am trying to use:</p> <pre><code>package main import ( "fmt" "log" "gopkg.in/mgo.v2" ) type Person struct { FirstName string `bson: "firstName" json: "firstName"` LastName string `bson: "lastName json: "lastName"` } func main() { session, err := mgo.Dial("10.0.0.89") if err != nil { panic(err) } defer session.Close() // Optional. Switch the session to a monotonic behavior. session.SetMode(mgo.Monotonic, true) c := session.DB("PeopleDatabase").C("People") var people []Person err = c.Find(nil).All(&people) if err != nil { log.Fatal(err) } for _, res := range people{ fmt.Printf("Name: %v ", res) } } </code></pre> <p>When I run this code I get the following Output:</p> <pre><code>Name: { } Name: { } Name: { } </code></pre> <p>When using res.FirstName in place of res I just get a space in lieu of the {}.</p> <p>I have been over the documentation in the following locations:</p> <p><a href="https://labix.org/mgo" rel="nofollow">https://labix.org/mgo</a></p> <p><a href="https://godoc.org/gopkg.in/mgo.v2#Collection.Find" rel="nofollow">https://godoc.org/gopkg.in/mgo.v2#Collection.Find</a></p> <p><a href="https://gist.github.com/border/3489566" rel="nofollow">https://gist.github.com/border/3489566</a></p> <p>I would be extemely grateful for any help that can be given. Thank You.</p> </div>
<div class="post-text" itemprop="text"> <div class="question-status question-originals-of-duplicate"> <p>This question already has an answer here:</p> <ul> <li> <a href="/questions/38093012/printing-empty-json-as-a-result" dir="ltr">Printing Empty Json as a result [duplicate]</a> <span class="question-originals-answer-count"> 1 answer </span> </li> <li> <a href="/questions/45508642/golang-mgo-getting-empty-objects" dir="ltr">Golang mgo getting empty objects</a> <span class="question-originals-answer-count"> 1 answer </span> </li> </ul> </div> <p>I'm trying to get data from mongodb to golang program. But I don't know why the received data is not full.</p> <pre><code>package main import ( "context" "fmt" "log" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) func main() { ctx := context.Background() clientOptions := options.Client().ApplyURI("mongodb://localhost:27017") client, err := mongo.Connect(ctx, clientOptions) if err != nil { log.Fatal(err) } err = client.Ping(ctx, nil) if err != nil { log.Fatal(err) } fmt.Println("Connected to MongoDB!") db := client.Database("testDB") col := db.Collection("testcs") type Dataa struct { ID bson.RawValue `bson:"_id"` data string `bson:"data"` number int `bson:"number"` } filter := bson.M{"number": 2} var p Dataa if err := col.FindOne(ctx, filter).Decode(&p); err != nil { log.Fatal(err) } fmt.Printf("post: %+v ", p) } </code></pre> <p>Receiced data is: post: {ID:{"$oid":"5cf64ca8d95df708be0ed503"} data: number:0}</p> <p>But the "data" value should be "ABCEX" and "number" value should be 2 like what I have addded into mongodb.</p> <p>Could you guys please help me to find out the problem. Thank you!</p> </div>
今天在做mongodb3.0.1与java项目整合时候,发现后台总是在打印debug信息 2015-08-11 17:32:24.621 [cluster-ClusterId{value='55c9c11e80df810c54492c6b', description='null'}-localhost:27017] DEBUG o.mongodb.driver.cluster.debug(56) - Checking status of localhost:27017 2015-08-11 17:32:24.624 [cluster-ClusterId{value='55c9c11e80df810c54492c6b', description='null'}-localhost:27017] DEBUG o.mongodb.driver.cluster.debug(56) - Updating cluster description to {type=STAN DALONE, servers=[{address=localhost:27017, type=STANDALONE, roundTripTime=0.9 ms, state=CONNECTED}] 2015-08-11 17:32:25.363 [cluster-ClusterId{value='55c9c11f80df810c54492c6c', description='null'}-localhost:27017] DEBUG o.mongodb.driver.cluster.debug(56) - Checking status of localhost:27017 2015-08-11 17:32:25.365 [cluster-ClusterId{value='55c9c11f80df810c54492c6c', description='null'}-localhost:27017] DEBUG o.mongodb.driver.cluster.debug(56) - Updating cluster description to {type=STAN DALONE, servers=[{address=localhost:27017, type=STANDALONE, roundTripTime=0.6 ms, state=CONNECTED}] 2015-08-11 17:32:34.626 [cluster-ClusterId{value='55c9c11e80df810c54492c6b', description='null'}-localhost:27017] DEBUG o.mongodb.driver.cluster.debug(56) - Checking status of localhost:27017 2015-08-11 17:32:34.628 [cluster-ClusterId{value='55c9c11e80df810c54492c6b', description='null'}-localhost:27017] DEBUG o.mongodb.driver.cluster.debug(56) - Updating cluster description to {type=STAN DALONE, servers=[{address=localhost:27017, type=STANDALONE, roundTripTime=1.0 ms, state=CONNECTED}] 2015-08-11 17:32:35.367 [cluster-ClusterId{value='55c9c11f80df810c54492c6c', description='null'}-localhost:27017] DEBUG o.mongodb.driver.cluster.debug(56) - Checking status of localhost:27017 2015-08-11 17:32:35.369 [cluster-ClusterId{value='55c9c11f80df810c54492c6c', description='null'}-localhost:27017] DEBUG o.mongodb.driver.cluster.debug(56) - Updating cluster description to {type=STAN DALONE, servers=[{address=localhost:27017, type=STANDALONE, roundTripTime=0.7 ms, state=CONNECTED}] 2015-08-11 17:32:44.629 [cluster-ClusterId{value='55c9c11e80df810c54492c6b', description='null'}-localhost:27017] DEBUG o.mongodb.driver.cluster.debug(56) - Checking status of localhost:27017 2015-08-11 17:32:44.631 [cluster-ClusterId{value='55c9c11e80df810c54492c6b', description='null'}-localhost:27017] DEBUG o.mongodb.driver.cluster.debug(56) - Updating cluster description to {type=STAN DALONE, servers=[{address=localhost:27017, type=STANDALONE, roundTripTime=1.1 ms, state=CONNECTED}] 2015-08-11 17:32:45.370 [cluster-ClusterId{value='55c9c11f80df810c54492c6c', description='null'}-localhost:27017] DEBUG o.mongodb.driver.cluster.debug(56) - Checking status of localhost:27017 2015-08-11 17:32:45.372 [cluster-ClusterId{value='55c9c11f80df810c54492c6c', description='null'}-localhost:27017] DEBUG o.mongodb.driver.cluster.debug(56) - Updating cluster description to {type=STAN DALONE, servers=[{address=localhost:27017, type=STANDALONE, roundTripTime=0.8 ms, state=CONNECTED}] ``` ``` 请问如何关闭mongodb的这个debug信息打印
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页