读书人

MongoDB学习小结-入门篇

发布时间: 2012-08-26 16:48:06 作者: rapoo

MongoDB学习总结--入门篇

一、MongoDB简介

?

??????MongoDB是一个高性能,开源,无模式的文档型数据库,是当前
?

?????? 如图显示MongoDB数据库服务已经成功启动了,在浏览器输入:http://localhost:27017/,可以看到如下提示:

You are trying to access MongoDB on the native driver port. For http diagnostic access, add 
1000 to the port number。

?

三、MongoDB Shell Operations?

?

????? shell命令操作语法和JavaScript很类似,其实控制台底层的查询语句都是用JavaScript脚本完成操作的。

?

Help> coll.findOne( criteria ); //Find and return a single object. Returns null if not found. If you want only one object returned, this is more efficient than just find() as limit(1) is implied. You may use regular expressions if the element type is a string, number, or date: coll.find( { name: /joe/i } ); > coll.find( criteria, fields ); //Get just specific fields from the object. E.g.: coll.find( {}, {name:true} ); > coll.find().sort( {field:1[, field:1] }); //Return results in the specified order (field ASC). Use -1 for DESC. > coll.find( criteria ).sort( { field : 1 } ) //Return the objects matching criteria, sorted by field. > coll.find( ... ).limit(n) //Limit result to n rows. Highly recommended if you need only a certain number of rows for best performance. > coll.find( ... ).skip(n ) //Skip n results. > coll.count() //Returns total number of objects in the collection. > coll.find( ... ).count() //Returns the total number of objects that match the query. Note that the number ignores limit and skip; for example if 100 records match but the limit is 10, count() will return 100. This will be faster than iterating yourself, but still take time.

More information: see queries.

Error Checking> db.getLastError() //Returns error from the last operation. > db.getPrevError() //Returns error from previous operations. > db.resetError() //Clear error memory. Administrative Command Helpers> db.cloneDatabase(fromhost) //Clone the current database from the other host specified. fromhost database must be in noauth mode. > db.copyDatabase(fromdb, todb, fromhost) //Copy fromhost/fromdb to todb on this server. fromhost must be in noauth mode. > db.fromColl.renameCollection(toColl) //Rename collection from fromColl to toColl. > db.repairDatabase() //Repair and compact the current database. This operation can be very slow on large databases. > db.addUser(user,pwd) //Add user to current database. > db.getCollectionNames() //get list of all collections. > db.dropDatabase() //Drops the current database. Opening Additional Connections> db = connect("<host>:<port>/<dbname>") //Open a new database connection. One may have multiple connections within a single shell, however, automatic getLastError reporting by the shell is done for the 'db' variable only.> conn = new Mongo("hostname") //Open a connection to a new server. Use getDB() to select a database thereafter. > db = conn.getDB("dbname") //Select a specific database for a connection Miscellaneous> Object.bsonsize(db.foo.findOne()) //prints the bson size of a db object (mongo version 1.3 and greater) > db.foo.findOne().bsonsize() //prints the bson size of a db object (mongo versions predating 1.3) ?五、资料链接1、http://www.mongodb.org/display/DOCS/Overview+-+The+MongoDB+Interactive+Shell 2、http://www.mongodb.org/display/DOCS/dbshell+Reference

读书人网 >其他数据库

热点推荐