`
canofy
  • 浏览: 836162 次
  • 性别: Icon_minigender_1
  • 来自: 北京、四川
社区版块
存档分类
最新评论

mongo的初体验

阅读更多
首先在mongo官网下载Windows的版本

启动服务:mongod.exe --port 12345 --dbpath=c:\mongodb\db
显示一下信息:
Fri Dec 04 14:30:32 Mongo DB : starting : pid = 0 port = 12345 dbpath = c:\mongo
db\db master = 0 slave = 0  32-bit

** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data

**       see http://blog.mongodb.org/post/137788967/32-bit-limitations for more

Fri Dec 04 14:30:32 db version v1.1.4-, pdfile version 4.5
Fri Dec 04 14:30:32 git version: c67c2f7dd681152f1784c8e1c2119b979e65881d
Fri Dec 04 14:30:32 sys info: windows (5, 1, 2600, 2, 'Service Pack 3') BOOST_LI
B_VERSION=1_35
Fri Dec 04 14:30:32 waiting for connections on port 12345


启动客户端:mongo.exe localhost:12345

检查是否正常:db.foo.save({a:1,b:9})
     db.foo.find({a:1})
控制台显示:{ "_id" : ObjectId("4b18b5b56f40000000006cec"), "a" : 1, "b" : 9 }


添加数据:db.foo.save({a:1,b:9})

查询数据:db.foo.find({a:1})  //{a:1}是查询条件,当为空时查询所有
  db.foo.findOne({a:1}) //显示出一条数据

删除数据:db.foo.remove({a:1}) //删除a=1的数据

表的数据量:db.foo.find().count()

显示数据指定的条数:db.foo.find().limit(n)

显示库名:db.foo.getDB()

获取索引值:db.foo.getIndexes()

表的统计:db.foo.stats()

删除表:db.foo.drop()

获取不重复的列:db.foo.distinct( key ) - eg. db.foo.distinct( 'x' )

忽略前面的几行:db.other.find({a:2}).skip(2)//忽略a=2中的前面2行

方法帮助:db.foo.help()


HELP
        show dbs                     show database names
        show collections             show collections in current database
        show users                   show users in current database
        show profile                 show most recent system.profile entries with time >= 1ms
        use <db name>                set curent database to <db name>
        db.help()                    help on DB methods
        db.foo.help()                help on collection methods
        db.foo.find()                list objects in collection foo
        db.foo.find( { a : 1 } )     list objects in foo where a == 1
        it                           result of the last line evaluated; use to further iterate


DB methods:
        db.addUser(username, password)
        db.auth(username, password)
        db.cloneDatabase(fromhost)
        db.commandHelp(name) returns the help for the command
        db.copyDatabase(fromdb, todb, fromhost)
        db.createCollection(name, { size : ..., capped : ..., max : ... } )
        db.currentOp() displays the current operation in the db
        db.dropDatabase()
        db.eval(func, args) run code server-side
        db.getCollection(cname) same as db['cname'] or db.cname
        db.getCollectionNames()
        db.getLastError() - just returns the err msg string
        db.getLastErrorObj() - return full status object
        db.getMongo() get the server connection object
        db.getMongo().setSlaveOk() allow this connection to read from the nonmaster member of a replica pair
        db.getName()
        db.getPrevError()
        db.getProfilingLevel()
        db.getReplicationInfo()
        db.getSisterDB(name) get the db at the same server as this onew
        db.killOp() kills the current operation in the db
        db.printCollectionStats()
        db.printReplicationInfo()
        db.printSlaveReplicationInfo()
        db.printShardingStatus()
        db.removeUser(username)
        db.repairDatabase()
        db.resetError()
        db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj : 1 }
        db.setProfilingLevel(level) 0=off 1=slow 2=all
        db.shutdownServer()
        db.version() current version of the server

DBCollection help
        db.foo.count()
        db.foo.dataSize()
        db.foo.distinct( key ) - eg. db.foo.distinct( 'x' )
        db.foo.drop() drop the collection
        db.foo.dropIndex(name)
        db.foo.dropIndexes()
        db.foo.ensureIndex(keypattern,options) - options should be an object with these possible fields: name, unique, dropDups
        db.foo.find( [query] , [fields]) - first parameter is an optional queryfilter. second parameter is optional set of fields to return.  e.g. db.foo.find( { x : 77 } , { name : 1 , x : 1 } )
        db.foo.find(...).count()
        db.foo.find(...).limit(n)
        db.foo.find(...).skip(n)
        db.foo.find(...).sort(...)
        db.foo.findOne([query])
        db.foo.getDB() get DB object associated with collection
        db.foo.getIndexes()
        db.foo.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
        db.foo.mapReduce( mapFunction , reduceFunction , <optional params> )
        db.foo.remove(query)
        db.foo.renameCollection( newName ) renames the collection
        db.foo.save(obj)
        db.foo.stats()
        db.foo.storageSize() - includes free space allocated to this collection
        db.foo.totalIndexSize() - size in bytes of all the indexes
        db.foo.totalSize() - storage allocated for all data and indexes
        db.foo.update(query, object[, upsert_bool])
        db.foo.validate() - SLOW
        db.foo.getShardVersion() - only for use with sharding
分享到:
评论

相关推荐

    nosqlbooster4mongo-5.2.8.zip

    NosqlBooster 5.2.8是该工具的一个版本,提供了一系列增强的功能和优化的用户体验。 NosqlBooster 4Mongo的主要特点包括: 1. **用户友好的界面**:它采用直观的GUI设计,使用户能够轻松地浏览、查询、编辑和管理...

    mongo-linux,nosqlbooster.zip

    MongoDB是一种流行的开源文档型数据库,属于NoSQL数据库范畴,以其灵活性、高性能和高可扩展性而备受青睐。"mongo-linux,nosqlbooster....对于初次接触MongoDB或希望优化工作流程的人来说,这是一个非常有价值的资源。

    mongodb初体验

    MongoDB初体验:深入理解NoSQL数据库的魅力 MongoDB是一种流行的开源、分布式文档数据库,它属于NoSQL数据库的一种。在当今大数据和云计算的时代背景下,MongoDB因其灵活性、高性能和易于扩展性而备受青睐。本文将...

    电影微服务

    ReactJS 是React的一个版本,专门用于构建服务器端渲染(SSR)的应用,它可以帮助优化SEO并提高初次加载页面的速度。在电影微服务中,ReactJS可以用于生成预渲染的HTML,提高用户体验。 JavaScript 作为Web开发的...

    simple-todo-list:第一次尝试 Meteor.js

    在当今快速发展的Web开发领域,JavaScript框架不断涌现,其中Meteor.js以其独特的全栈式开发体验,吸引了众多开发者的眼球。本项目"simple-todo-list"正是基于Meteor.js进行的初次尝试,旨在通过构建一个简单的待办...

Global site tag (gtag.js) - Google Analytics