Esempio n. 1
0
func testP() {
	go func() {
		mgox.New("111111").Insert(
			&User{Name: "xiongtoto456", Age: 8, Sex: 1, FirstCreated: time.Now()},
		)
	}()
}
Esempio n. 2
0
func TestInsert(t *testing.T) {
	err := mgox.New("111").Insert(
		&User{Name: "xiongtoto456", Age: 8, Sex: 1},
	)
	println("success...xht")
	if handleError(t, err) {
		return
	}

	results := []bson.M{}
	mgox.New().Find(bson.M{}).Result(&results)

	if len(results) > 0 {
		output, _ := json.MarshalIndent(results, "", " ")
		println("result is :" + string(output))
	}

}
Esempio n. 3
0
/*
reference:
- https://godoc.org/labix.org/v2/mgo
- https://docs.mongodb.org/manual/tutorial/map-reduce-examples/

1. prepare data in test-db:
db.orders.save({_id: ObjectId("50a8240b927d5d8b5891743c"),cust_id: "abc123",ord_date: new Date("Oct 04, 2012"),status: 'A',price: 25,items: [ { sku: "ss1", qty: 5, price: 2.5 },{ sku: "nnn", qty: 5, price: 2.5 } ]})
db.orders.save({_id: ObjectId("50a8240b927d5d8b5891744c"),cust_id: "abc123",ord_date: new Date("Oct 04, 2012"),status: 'A',price: 21,items: [ { sku: "sss2", qty: 5, price: 2.5 },{ sku: "nnn", qty: 5, price: 2.5 } ]});
db.orders.save({_id: ObjectId("50a8240b927d5d8b5891745c"),cust_id: "abc124",ord_date: new Date("Oct 04, 2012"),status: 'B',price: 29,items: [ { sku: "sss3", qty: 5, price: 2.5 },{ sku: "nnn", qty: 5, price: 2.5 } ]});
db.orders.save({_id: ObjectId("50a8240b927d5d8b5891746c"),cust_id: "abc125",ord_date: new Date("Oct 04, 2012"),status: 'A',price: 11,items: [ { sku: "sss4", qty: 5, price: 2.5 },{ sku: "nnn", qty: 5, price: 2.5 } ]});

2. execute in mongo-shell
var mapFunction1 = function() {
                       emit(this.cust_id, this.price);
                   };
var reduceFunction1 = function(keyCustId, valuesPrices) {
                       return Array.sum(valuesPrices);
                   };

db.orders.mapReduce(
                     mapFunction1,
                     reduceFunction1,
                     { out: "map_reduce_example" }
                   )

3. show the shell result
{
	"result" : "map_reduce_example",
	"timeMillis" : 117,
	"counts" : {
		"input" : 4,
		"emit" : 4,
		"reduce" : 1,
		"output" : 3
	},
	"ok" : 1
}
*/
func TestMapReduce(t *testing.T) {

	println("aa:", 1<<5)

	dialInfo := &mgo.DialInfo{
		Addrs:    []string{"127.0.0.1"},
		Timeout:  10 * time.Second,
		Database: "tako",
		Username: "******",
		Password: "******",
	}

	// Connect to MongoDB and establish a connection
	session, err := mgo.DialWithInfo(dialInfo)
	if err != nil {
		println("db create ERROR : %s", err)
		return
	}

	println("db create success")

	// Capture a reference to the collection
	db := session.DB(MONGODB_DATABASE)

	job := &mgo.MapReduce{
		Map:    "function() {emit(this.cust_id, this.price);};",
		Reduce: "function(keyCustId, valuesPrices) {return Array.sum(valuesPrices);};",
	}

	result := []bson.M{}

	_, err1 := db.C("orders").Find(nil).MapReduce(job, &result)
	if err1 != nil {
		return
	}
	if len(result) > 0 {
		output, _ := json.MarshalIndent(result, "", " ")
		println("map reduce result is :" + string(output))
	}

	count := 1000000
	for i := 0; i < count; i++ {
		//Push(randIp(), nil, nil)
		go func() {
			d := mgox.Connect()
			defer d.Close()
			mgox.New("111111").Insert(
				&User{Name: "xiongtoto456", Age: 8, Sex: 1, FirstCreated: time.Now()},
			)
		}()
	}

}
Esempio n. 4
0
func TestTimezoneInsert(t *testing.T) {
	err := mgox.New("111111").Insert(
		&User{Name: "xiongtoto456", Age: 8, Sex: 1, FirstCreated: time.Now()},
	)

	println("success...xht")
	if handleError(t, err) {
		return
	}

	var usr User
	mgox.New().Find(bson.M{"name": "xiongtoto456"}).Result(&usr)

	println(usr.FirstCreated.String())

	//	if len(results) > 0 {
	//		output, _ := json.MarshalIndent(results, "", " ")
	//		println("result is :" + string(output))
	//	}

}
Esempio n. 5
0
func TestUpdate(t *testing.T) {
	db, _ := mgox.GetDatabase()

	o1 := bson.M{
		//		"ip" : "113.106.106.98",
		"_id":            bson.ObjectIdHex("5656b4833e5a2c071d00005b"),
		"client.country": bson.M{"$exists": true},
	}

	o2 := bson.M{"$set": bson.M{
		"client.country": "china111",
		"client.area":    "华南1",
		//		"client.region":"广东1",
		"client.city": "珠海",
		"client.hee":  "11112",
	}}
	//	o2 := bson.M{"$set":bson.M{"client":bson.M{
	//		"country": "china111",
	//		"area": "华南",
	//		"region":"广东1",
	//		"city":"珠海",
	//		"hee":"1111",
	//	}}}

	results := []bson.M{}

	//	_ = mgox.Dao().Set("downloadlog", o1, o2)

	db.C("downloadlog").UpdateAll(o1, o2)
	db.C("downloadlog").Find(o1).All(&results)

	db.C("app").UpdateAll(bson.M{"appid": "55bb3026e138230ee700000e"}, bson.M{"$set": bson.M{"downloadcount": 11, "viewcount": 555}})
	//	db.C("").Pipe()

	if len(results) > 0 {
		output, _ := json.MarshalIndent(results, "", " ")
		println("update result is :" + string(output))
	}

	var ips []string
	mgox.New().Find().Distinct("downloadlog", "client.ip", &ips)
	//	db.C("downloadlog").UpdateId(bson.ObjectIdHex("5656b4e13e5a2c071d00005d"),o2)

}