Use of MongoDB stored procedures and performance tuning Solutions
Although MongoDB provides us with a lot of drivers, it is not convenient to use the mongodb shell. For example, the recently needed DBRef nested data must be CRUD. If the mog driver is used, it will be very troublesome. Therefore, we will conduct an experiment here. First, add the Server-side script and test the initialization data to the test database.
Mongo add data
Db. people. insert ({"_ id": "test", "phone", "233333"}) // enter the original data
// Database modification function
// After update, the modified data is returned.
PeopleUpdate = function (id, phone ){
Db. people. update ({
"_ Id": id,
},{ $ Set :{
Phone: phone
}});
Return db. people. findOne ({"_ id": id });
}
// Add database functions
Db. system. js. insert ({"_ id": "leleupdatephone", "value": peopleUpdate });
// Modify Database Functions
Db. system. js. update ({"_ id": "leleupdatephone" },{$ set: {"value": leleupdate }})
// Execute database functions
Db. eval ("leleupdatephone ('test', '23333test ')")
After the test, it is OK in mongo shell.
Golang calls the function and returns
Package main
Import (
"Bkjia.org/v2/mgo"
"Bkjia.org/v2/mgo/bson"
)
Func main (){
Session, err: = mgo. Dial ("")
If err! = Nil {
Panic (err)
}
Defer session. Close ()
Session. SetMode (mgo. Monotonic, true)
Db: = session. DB ("test ")
Var result interface {}
Db. Run (bson. M {"eval": "leleupdatephone ('test', 'new test');"}, & result)
}
As shown in the code above, You can execute modifications.
But pay attention to the lock issue: eval will generate the write lock. You can understand the results.
Performance testing.
I directly tested calling the main function 10000 times.
1. eval1. eval is not doing well in the test because the database will be locked.
2. By the way, the mongodb lock is quite large.
3. It takes four seconds for me to wait until the result is tested and even modify the result several times. (Because the previous lock is not opened .)
2. runCommand1. it will be much faster
The update operation for the same key is about 3 s.
Summary
1. I am using MBP MGX82.
2. Although the performance is not very good (much worse than MYSql), it is enough to support general applications.
3. MongoDB is not violent, just because I don't understand it.
CentOS compilation and installation of MongoDB
CentOS compilation and installation of php extensions for MongoDB and mongoDB
CentOS 6 install MongoDB and server configuration using yum
Install MongoDB2.4.3 in Ubuntu 13.04
MongoDB beginners must read (both concepts and practices)
MongoDB Installation Guide for Ubunu 14.04
MongoDB authoritative Guide (The Definitive Guide) in English [PDF]
Nagios monitoring MongoDB sharded cluster service practice
Build MongoDB Service Based on CentOS 6.5 Operating System
MongoDB details: click here
MongoDB: click here
This article permanently updates the link address: