楼主: jieforest

Introduction to MongoDB for Java, PHP and Python Developers

[复制链接]
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
31#
 楼主| 发表于 2012-6-6 08:35 | 只看该作者
Figure 8: Adding Mongo jar file to your project



Once you have it all setup, working with Java and MongoDB is quite easy as shown in figure 9.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
32#
 楼主| 发表于 2012-6-6 08:36 | 只看该作者
Figure 9 Using MongoDB from Eclipse



The above is roughly equivalent to the console/JavaScript code that we were doing earlier. The BasicDBObject is a type of Map with some convenience methods added. The DBCursor is like a JDBC ResultSet. You execute queries with DBColleciton.

There is no query syntax, just finder methods on the collection object. The output from the above is:
  1. Out:
  2. { "_id" : { "$oid" : "4f964d3000b5874e7a163895"} , "name" : "Rick
  3. Hightower" , "gender" : "m" , "phone" : "520-555-1212" ,
  4. "age" : 42.0}
  5. { "_id" : { "$oid" : "4f984cce72320612f8f432bb"} , "name" : "Diana
  6. Hightower" , "gender" : "f" , "phone" : "520-555-1212" ,
  7. "age" : 30}
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
33#
 楼主| 发表于 2012-6-6 08:37 | 只看该作者
Once you create some documents, querying for them is quite simple as show in figure 10.

Figure 10: Using Java to query MongoDB



The output from figure 10 is as follows:
  1. Rick?
  2. { "_id" : { "$oid" : "4f964d3000b5874e7a163895"} , "name" : "Rick
  3. Hightower" , "gender" : "m" , "phone" : "520-555-1212" ,
  4. "age" : 42.0}
  5. Diana?
  6. { "_id" : { "$oid" : "4f984cae72329d0ecd8716c8"} , "name" : "Diana
  7. Hightower" , "gender" : "f" , "phone" : "520-555-1212" ,
  8. "age" : 30}

  9. Diana by object id?
  10. { "_id" : { "$oid" : "4f984cce72320612f8f432bb"} , "name" : "Diana
  11. Hightower" , "gender" : "f" , "phone" : "520-555-1212" ,
  12. "age" : 30}
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
34#
 楼主| 发表于 2012-6-7 04:24 | 只看该作者
Just in case anybody wants to cut and paste any of the above, here it is again all in one go in the following listing.

Listing: Complete Java Listing
  1. package com.mammatustech.mongo.tutorial;

  2. import org.bson.types.ObjectId;

  3. import com.mongodb.BasicDBObject;
  4. import com.mongodb.DBCollection;
  5. import com.mongodb.DBCursor;
  6. import com.mongodb.DBObject;
  7. import com.mongodb.Mongo;
  8. import com.mongodb.DB;

  9. public class Mongo1Main {
  10.         public static void main (String [] args) throws Exception {
  11.                 Mongo mongo = new Mongo();
  12.                 DB db = mongo.getDB("tutorial");
  13.                 DBCollection employees = db.getCollection("employees");
  14.                 employees.insert(new BasicDBObject().append("name", "Diana Hightower")
  15.                   .append("gender", "f").append("phone", "520-555-1212").append("age", 30));
  16.                 DBCursor cursor = employees.find();
  17.                 while (cursor.hasNext()) {
  18.                         DBObject object = cursor.next();
  19.                         System.out.println(object);
  20.                 }
  21.                
  22.                 //> db.employees.find({name:"Rick Hightower"})
  23.                 cursor=employees.find(new BasicDBObject().append("name", "Rick Hightower"));
  24.                 System.out.printf("Rick?\n%s\n", cursor.next());
  25.                
  26.                 //> db.employees.find({age:{$lt:35}})       
  27.                 BasicDBObject query = new BasicDBObject();
  28.                 query.put("age", new BasicDBObject("$lt", 35));
  29.                 cursor=employees.find(query);
  30.                 System.out.printf("Diana?\n%s\n", cursor.next());
  31.                
  32.                 //> db.employees.findOne({_id : ObjectId("4f984cce72320612f8f432bb")})
  33.                 DBObject dbObject = employees.findOne(new BasicDBObject().append("_id",
  34.                                 new ObjectId("4f984cce72320612f8f432bb")));
  35.                 System.out.printf("Diana by object id?\n%s\n", dbObject);
  36.                
  37.                

  38.         }
  39. }
复制代码
Please note that the above is completely missing any error checking, or resource cleanup. You will need do some of course (try/catch/finally, close connection, you know that sort of thing).

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
35#
 楼主| 发表于 2012-6-7 04:26 | 只看该作者
Python MongoDB Setup

Setting up Python and MongoDB are quite easy since Python has its own package manager.

To install mongodb lib for Python MAC OSX, you would do the following:
  1. $ sudo env ARCHFLAGS='-arch i386 -arch x86_64'
  2. $ python -m easy_install pymongo
复制代码
To install Python MongoDB on Linux or Windows do the following:
  1. $ easy_install pymongo
复制代码
or
  1. $ pip install pymongo
复制代码
If you don't have easy_install on your Linux box you may have to do some sudo apt-get install python-setuptools or sudo yum install python-setuptools iterations, although it seems to be usually installed with most Linux distributions these days.

If easy_install or pip is not installed on Windows, try reformatting your hard disk and installing a real OS, or if that is too inconvient go here.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
36#
 楼主| 发表于 2012-6-7 04:44 | 只看该作者
Once you have it all setup, you will can create some code that is equivalent to the first console examples as shown in figure 11.

Figure 11: Python code listing part 1
  1. import pymongo
  2. from bson.objectid import ObjectId
  3. connection = pymongo.Connection()
  4. db = connection["tutorial"]
  5. employees = db["employees"]
  6. employees.insert({"name":"Lucas Hightower",'gender':'m','phone':'520-555-1212','age':8})
  7. cursor = db.employees.find()
  8. for employee in db.employees.find()
  9.     print employee
复制代码
Python does have literals for maps so working with Python is much closer to the JavaScript/Console from earlier than Java is. Like Java there are libraries for Python that work with MongoDB (MongoEngine, MongoKit, and more).

Even executing queries is very close to the JavaScript experience as shown in figure 12.

Figure 12: Python code listing part 2
  1. print employees.find({"name":"Rick Hightower"})[0]
  2. # Output
  3. # {u'gender':u'm',u'age':42.0,u'_id':ObjectId('4f964d3000b5874e7a163895'),
  4. # u'name':u'Rick Hightower',u'phone':u'520-555-1212'}

  5. cursor = employees.find({"age":{"$lt":35}})
  6. for employee in cursor:
  7.         print "under 35: %s" % employee
  8. # Output
  9. #under 35: {u'gender':u'f',u'age': 30, u'_id':ObjectId("4f985a7f72323465ed25cccd'),u'name':
  10. #u'Diana Hightower', u'phone':u'520-555-1212'}
  11. #under 35:{u'gender':u'm',u'age':8,u'_id':ObjectId('4f9e111980cbd54eea000000'),u'name':
  12. #u'Lucas Hightower', u'phone':u'520-555-1212'}

  13. diana = employees.find_one({"_id":ObjectId("4f984cce72320612f8f432bb")})
  14. print "Diana %s" % diana
  15. #Output
  16. #Diana {u'gender': u'f', u'age':30, u'_id':ObjectId('4f984cce72320612f8f432bb'),
  17. #u'name': u'Diana Hightower', u'phone': u'520-555-1212'}
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
37#
 楼主| 发表于 2012-6-7 04:45 | 只看该作者
Here is the complete listing to make the cut and paste crowd (like me), happy.

Listing: Complete Python listing
  1. import pymongo
  2. from bson.objectid import ObjectId


  3. connection = pymongo.Connection()

  4. db = connection["tutorial"]
  5. employees = db["employees"]

  6. employees.insert({"name": "Lucas Hightower", 'gender':'m', 'phone':'520-555-1212', 'age':8})

  7. cursor = db.employees.find()
  8. for employee in db.employees.find():
  9.     print employee


  10. print employees.find({"name":"Rick Hightower"})[0]


  11. cursor = employees.find({"age": {"$lt": 35}})
  12. for employee in cursor:
  13.      print "under 35: %s" % employee


  14. diana = employees.find_one({"_id":ObjectId("4f984cce72320612f8f432bb")})
  15. print "Diana %s" % diana
复制代码
The output for the Python example is as follows:
  1. {u'gender': u'm', u'age': 42.0, u'_id': ObjectId('4f964d3000b5874e7a163895'), u'name': u'Rick Hightower', u'phone':
  2. u'520-555-1212'}

  3. {u'gender': u'f', u'age': 30, u'_id': ObjectId('4f984cae72329d0ecd8716c8'), u'name': u'Diana Hightower', u'phone':
  4. u'520-555-1212'}

  5. {u'gender': u'm', u'age': 8, u'_id': ObjectId('4f9e111980cbd54eea000000'), u'name': u'Lucas Hightower', u'phone':
  6. u'520-555-1212'}
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
38#
 楼主| 发表于 2012-6-7 04:46 | 只看该作者
All of the above but in PHP

Node.js , Ruby, and Python in that order are the trend setter crowd in our industry circa 2012. Java is the corporate crowd, and PHP is the workhorse of the Internet. The "get it done" crowd. You can't have a decent NoSQL solution without having good PHP support.

To install MongoDB support with PHP use pecl as follows:
  1. $ sudo pecl install mongo
复制代码
Add the mongo.so module to php.ini.
  1. extension=mongo.so
复制代码
Then assuming you are running it on apache, restart as follows:
  1. $ apachectl stop
  2. $ apachectl start
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
39#
 楼主| 发表于 2012-6-8 06:12 | 只看该作者
Figure 13 shows our roughly equivalent code listing in PHP.

Figure 13 PHP code listing



The output for figure 13 is as follows:
  1. Output:
  2. array ( '_id' => MongoId::__set_state(array( '$id' => '4f964d3000b5874e7a163895', )), 'name' => 'Rick Hightower',
  3. 'gender' => 'm', 'phone' => '520-555-1212', 'age' => 42, )

  4. array ( '_id' => MongoId::__set_state(array( '$id' => '4f984cae72329d0ecd8716c8', )), 'name' => 'Diana Hightower', 'gender' => ‘f',
  5. 'phone' => '520-555-1212', 'age' => 30, )

  6. array ( '_id' => MongoId::__set_state(array( '$id' => '4f9e170580cbd54f27000000', )), 'gender' => 'm', 'age' => 8, 'name' => 'Lucas Hightower',
  7. 'phone' => '520-555-1212', )
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
40#
 楼主| 发表于 2012-6-8 06:13 | 只看该作者
The other half of the equation is in figure 14.

Figure 14 PHP code listing



The output for figure 14 is as follows:
  1. Output
  2. Rick?
  3. array ( '_id' => MongoId..., 'name' => 'Rick Hightower', 'gender' => 'm',
  4. 'phone' => '520-555-1212', 'age' => 42, )
  5. Diana?
  6. array ( '_id' => MongoId::..., 'name' => 'Diana Hightower', 'gender' => ‘f',
  7. 'phone' => '520-555-1212', 'age' => 30, )
  8. Diana by id?
  9. array ( '_id' => MongoId::..., 'name' => 'Diana Hightower', 'gender' => 'f',
  10. 'phone' => '520-555-1212', 'age' => 30, )
复制代码

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表