MongoDB 初探

MongoDB一会和大家聊一下!

 

一、集文档数据库,键值对存储和关系型数据库的优点于一身。做关系型数据库展示的绝佳选择!

MongoDB (名称来自"humongous") 是一个可扩展的,高性能,开源,模式自由,面向文档的数据库.使用C++编写,MongoDB特点:

  •     面向文档存储(类JSON数据模式简单而强大)------这个比较方便
  •      动态查询----NB的查询
  •      全索引支持,扩展到内部对象和内嵌数组
  •      查询记录分析
  •      快速,就地更新
  •      高效存储二进制大对象 (比如照片和视频)---这个不用呵呵
  •      复制和故障切换支持(1.6以上版本就支持分布式了)
  •     Auto-Sharding自动分片支持云级扩展性(分布式是最佳选择)
  •      MapReduce 支持复杂聚合
  •      商业支持,培训和咨询(呵呵,这个是大家用不着)

MongoDB 在键值存储(快速并有高扩展性) 和传统 RDBMS 系统(提供结构化模式和强大的查询)之间建立了一个桥梁。

 

二、安装配置  

我安装的版本: mongodb-win32-i386-1.7.0

将下载文件并解压,将文件放在了C:/Mongo/bin/

数据库默认设置放在C:/data/db/,需要手工创建文件夹

 

三、SHELL命令

Command Line 


--helpShow command line options
--nodbStart without a db, you can connect later with new Mongo() or connect()
--shellAfter running a .js file from the command line, stay in the shell rather than terminating

Special Command Helpers

Non-javascript convenience macros:

helpShow help
db.help()Show help on db methods
db.myColl.help()Show help on collection methods
show dbsPrint a list of all databases on this server
use dbnameSet the db variable to represent usage of dbname on the server
show collectionsPrint a list of all collections for current database
show usersPrint a list of users for current database
show profilePrint most recent profiling operations that took >= 1ms

Basic Shell Javascript Operations

dbThe variable that references the current database object / connection. Already defined for you in your instance.
db.auth(user,pass)Authenticate with the database (if running in secure mode).
coll = db.collectionAccess a specific collection within the database.
cursor = coll.find();Find all objects in the collection. See queries.
coll.remove(objpattern);Remove matching objects from the collection. 
objpattern is an object specifying fields to match. E.g.: coll.remove( { name: "Joe" } );
coll.save(object)Save an object in the collection, or update if already there. 
If your object has a presave method, that method will be called before the object is saved to the db (before both updates and inserts)
coll.insert(object) 
Insert object in collection.  No check is made (i.e., no upsert) that the object is not already present in the collection. 
coll.update(...) 
Update an object in a collection.  See the Updating documentation; update() has many options. 
coll.ensureIndex( { name: 1 } )Creates an index on tab.name. Does nothing if index already exists.
coll.update(...) 
coll.drop()Drops the collection coll
db.getSisterDB(name)Return a reference to another database using this same connection. Usage example:db.getSisterDB('production').getCollectionNames()

Queries

coll.find()Find all.
itContinue iterating the last cursor returned from find().
coll.find(criteria );Find objects matching criteria in the collection. E.g.: coll.find( { name: "Joe" } );
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.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. See here for an example of connect().
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)

 

 

 

 

 

四、其中.Net项目的调用很简单:

 

只需要引入MongoDB.dll 和 MongoDB.GridFS.dll 以及他们对应的xml说明即可。java版本正在研究中...

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值