楼主: 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
41#
 楼主| 发表于 2012-6-8 06:14 | 只看该作者
Here is the complete PHP listing.

PHP complete listing
  1. <!--?php

  2. $m = new Mongo();
  3. $db = $m->selectDB("tutorial");
  4. $employees = $db->selectCollection("employees");
  5. $cursor = $employees->find();

  6. foreach ($cursor as $employee) {
  7.   echo var_export ($employee, true) . "< br />";
  8. }

  9. $cursor=$employees->find( array( "name" => "Rick Hightower"));
  10. echo "Rick? < br /> " . var_export($cursor->getNext(), true);

  11. $cursor=$employees->find(array("age" => array('$lt' => 35)));
  12. echo "Diana? < br /> " . var_export($cursor->getNext(), true);

  13. $cursor=$employees->find(array("_id" => new MongoId("4f984cce72320612f8f432bb")));
  14. echo "Diana by id? < br /> " . var_export($cursor->getNext(), true);
  15. ?>
复制代码
If you like Object mapping to documents you should try the poorly named MongoLoid for PHP.

使用道具 举报

回复
论坛徽章:
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
42#
 楼主| 发表于 2012-6-8 06:16 | 只看该作者
Additional shell commands, learning about MongoDB via the console

One of the nice things that I appreciate about MongoDB, that is missing from some other NoSQL implementation, is getting around and finding information with the console application. They seem to closely mimic what can be done in MySQL so if you are familiar with MySQL, you will feel fairly at home in the mongo console.

To list all of the databases being managed by the mongod instance, you would do the following:
  1. > show dbs
  2. local        (empty)
  3. tutorial        0.203125GB
复制代码
To list the collections being managed by a database you could do this:
  1. > show collections
  2. employees
  3. system.indexes
复制代码
To show a list of users, you do the following:
  1. > show users
复制代码
To look at profiling and logging, you do this:
  1. > show profile
  2. db.system.profile is empty
  3. Use db.setProfilingLevel(2) will enable profiling
  4. ..

  5. > show logs
  6. global

  7. > show log global
  8. Mon Apr 23 23:33:14 [initandlisten] MongoDB starting :
  9.    pid=11773 port=27017 dbpath=/etc/mongodb/data 64-bit…

  10. Mon Apr 23 23:33:14 [initandlisten] options:
  11.    { config: "/etc/mongodb/mongodb.config", dbpath: "/etc/mongodb/data" }
复制代码

使用道具 举报

回复
论坛徽章:
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
43#
 楼主| 发表于 2012-6-9 02:09 | 只看该作者
Also the commands and JavaScript functions themselves have help associated with them. To see all of the operations that DBCollection supports you could do this:
  1. > db.employees.help()
  2. DBCollection help
  3.        
  4. db.employees.find().help() - show DBCursor help
  5. db.employees.count()
  6. db.employees.dataSize()
  7. db.employees.distinct( key ) - eg. db.employees.distinct( 'x' )
  8. db.employees.drop() drop the collection
  9. db.employees.dropIndex(name)
  10. db.employees.dropIndexes()
  11. db.employees.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups
  12. db.employees.reIndex()
  13. db.employees.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
  14.     e.g. db.employees.find( {x:77} , {name:1, x:1} )
  15. db.employees.find(...).count()
  16. db.employees.find(...).limit(n)
  17. db.employees.find(...).skip(n)
  18. db.employees.find(...).sort(...)
  19. db.employees.findOne([query])
  20. db.employees.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )
  21. db.employees.getDB() get DB object associated with collection
  22. db.employees.getIndexes()
  23. db.employees.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
  24. db.employees.mapReduce( mapFunction , reduceFunction , {optional params=""} )
  25. db.employees.remove(query)
  26. db.employees.renameCollection( newName , {droptarget} ) renames the collection.
  27. db.employees.runCommand( name , {options} ) runs a db command with the given name where the first param is the collection name
  28. db.employees.save(obj)
  29. db.employees.stats()
  30. db.employees.storageSize() - includes free space allocated to this collection
  31. db.employees.totalIndexSize() - size in bytes of all the indexes
  32. db.employees.totalSize() - storage allocated for all data and indexes
  33. db.employees.update(query, object[, upsert_bool, multi_bool])
  34. db.employees.validate( {full} ) – SLOW
  35. db.employees.getShardVersion() - only for use with sharding
  36. db.employees.getShardDistribution() - prints statistics about data distribution in the cluster ...
复制代码

使用道具 举报

回复
论坛徽章:
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
44#
 楼主| 发表于 2012-6-9 02:09 | 只看该作者
Just to show a snake eatings its own tail, you can even get help about help as follows:
  1. > help
  2.         db.help()                    help on db methods
  3.         db.mycoll.help()       help on collection methods
  4.         rs.help()                     help on replica set methods
  5.         help admin                administrative help
  6.         help connect             connecting to a db help
  7.         help keys                    key shortcuts
  8.         help misc                    misc things to know
  9.         help mr                       mapreduce
  10. > help

  11. show dbs                     show database names
  12. show collections        show collections in current database
  13. show users                  show users in current database
  14. show profile                show most recent system.profile entries time>= 1ms

  15. show logs                    show the accessible logger names
  16. show log [name]        prints out the last segment of log in memory,
复制代码
The MongoDB console reminds me of a cross between the MySQL console and Python's console. Once you use the console, it is hard to imagine using a NoSQL solution that does not have a decent console (hint, hint).

使用道具 举报

回复
论坛徽章:
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
45#
 楼主| 发表于 2012-6-9 02:10 | 只看该作者
Conclusion

The poorly named NoSQL movement seems to be here to stay. The need to have reliable storage that can be easily queried without the schema migration pain and scalability woes of traditional RDBMS is increasing. Developers want more agile systems without the pain of schema migration. MongoDB is a good first step for developers dipping their toe into the NoSQL solution space. It provides easy to use tools like its console and provides many language drivers. This article covered the basics of MongoDB architecture, caveats and how to program in MongoDB for Java, PHP, JavaScript and Python developers.

I hope you enjoyed reading this article 1/2 as much as I enjoyed writing it. --Rick Hightower

使用道具 举报

回复

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

本版积分规则 发表回复

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