HarrySun

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

I am focusing on this project:  http://www.goinggo.net/2013/12/sample-web-application-using-beego-and.html  this weekend.

 see:  http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/

1. install MongoDB

1) install MongoDB

2) start MongoDB as a services

run cmd as Administrator

mkdir c:\data\db
mkdir c:\data\log

echo logpath=c:\data\log\mongod.log> "C:\Program Files\MongoDB 2.6 Standard\mongod.cfg"
echo dbpath=c:\data\db>> "C:\Program Files\MongoDB 2.6 Standard\mongod.cfg"

sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB 2.6 Standard\bin\mongod.exe\" --service --config=\"C:\Program Files\MongoDB 2.6 Standard\mongod.cfg\"" DisplayName= "MongoDB 2.6 Standard" start= "auto"

 

If successfully created, the following log message will display:

[SC] CreateService SUCCESS

 

Start the MongoDB service.

net start MongoDB

Stop or remove the MongoDB service as needed.

To stop the MongoDB service, use the following command:

net stop MongoDB

To remove the MongoDB service, first stop the service and then run the following command:

sc.exe delete MongoDB

2. Mgo

1) download mgo , see http://labix.org/mgo

go get gopkg.in/mgo.v2

2) test mgo

 1 package main
 2 
 3 import (
 4         "fmt"
 5     "log"
 6         "gopkg.in/mgo.v2"
 7         "gopkg.in/mgo.v2/bson"
 8 )
 9 
10 type Person struct {
11         Name string
12         Phone string
13 }
14 
15 func main() {
16         session, err := mgo.Dial("server1.example.com,server2.example.com")
17         if err != nil {
18                 panic(err)
19         }
20         defer session.Close()
21 
22         // Optional. Switch the session to a monotonic behavior.
23         session.SetMode(mgo.Monotonic, true)
24 
25         c := session.DB("test").C("people")
26         err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
27                    &Person{"Cla", "+55 53 8402 8510"})
28         if err != nil {
29                 log.Fatal(err)
30         }
31 
32         result := Person{}
33         err = c.Find(bson.M{"name": "Ale"}).One(&result)
34         if err != nil {
35                 log.Fatal(err)
36         }
37 
38         fmt.Println("Phone:", result.Phone)
39 }

run and you will get the result as

Phone: +55 53 8116 9639

 

3. the whole project

 

Good luck!

posted on 2014-08-30 00:19  HarrySun  阅读(392)  评论(0编辑  收藏  举报