欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MongoDB最基本命令速查笔记

程序员文章站 2022-05-01 19:28:26
安装后在控制台输入: mongo启动客户端。 show dbs显示所有数据库。 use xxx选中数据库为当前使用的数据库。 show collections显示当...

安装后在控制台输入:

mongo启动客户端。

show dbs显示所有数据库。

use xxx选中数据库为当前使用的数据库。

show collections显示当前数据库的所有集合。

db.colleciton.find()来查找表格的所有数据。

db.serverstatus()查看数据库服务器的状态。

db.stats()查询指定数据库的统计信息。

use xxx如果数据库不存在则创建数据库(必须在虽然插入一条数据)

db.storecollection.insert({'version':'13', 'segment':'456'})插入数据,同时创建集合

db.dropdatabase()删除数据库

db.mycoll.drop()删除集合

db.storecollection.save({'version':'3.5', 'segment':'e3ol6'})更新记录

db.storecollection.remove({'version':'3.5'}) 删除记录

db.page.ensureindex({'title':1, 'url':-1})创建索引,1正序,-1逆序

db.page.getindexes()查询建立的索引

db.mycoll.dropindex(name) 删除索引,如果不带参数,删除所有索引

db.basese.reindex()重建索引

db.basese.storagesize()查询指定数据库的集合的可用的存储空间

db.basese.totalsize()查询集合已分配的存储空间

ps:非正常关闭后无法启动问题
段时间不小心非正常关闭了mongodb,准备重启却发现不行,错误信息如下:

tue apr 17 11:13:30
tue apr 17 11:13:30 warning: 32-bit servers don't have journaling enabled by default. please use --journal if you want durability.
tue apr 17 11:13:30
tue apr 17 11:13:30 [initandlisten] mongodb starting : pid=7128 port=27017 dbpath=d:\programs\mongodb204\db 32-bit host=falcon
tue apr 17 11:13:30 [initandlisten]
tue apr 17 11:13:30 [initandlisten] ** note: when using mongodb 32 bit, you are limited to about 2 gigabytes of data
tue apr 17 11:13:30 [initandlisten] **    see http://blog.mongodb.org/post/137788967/32-bit-limitations
tue apr 17 11:13:30 [initandlisten] **    with --journal, the limit is lower
tue apr 17 11:13:30 [initandlisten]
tue apr 17 11:13:30 [initandlisten] db version v2.0.4, pdfile version 4.5
tue apr 17 11:13:30 [initandlisten] git version: 329f3c47fe8136c03392c8f0e548506cb21f8ebf
tue apr 17 11:13:30 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=0, build=6002, platform=2, service_pack='service pack 2')boost_lib_version=1_42
tue apr 17 11:13:30 [initandlisten] options: { dbpath: "d:\programs\mongodb204\db" }
**************
unclean shutdown detected.
please visit http://dochub.mongodb.org/core/repair for recovery instructions.
*************
tue apr 17 11:13:30 [initandlisten] exception in initandlisten: 12596 old lock file, terminating
tue apr 17 11:13:30 dbexit:
tue apr 17 11:13:30 [initandlisten] shutdown: going to close listening sockets...
tue apr 17 11:13:30 [initandlisten] shutdown: going to flush diaglog...
tue apr 17 11:13:30 [initandlisten] shutdown: going to close sockets...
tue apr 17 11:13:30 [initandlisten] shutdown: waiting for fs preallocator...
tue apr 17 11:13:30 [initandlisten] shutdown: closing all files...
tue apr 17 11:13:30 [initandlisten] closeallfiles() finished
tue apr 17 11:13:30 dbexit: really exiting now

解决方法:
1、删除%mongo_home%/db下的.lock文件

2、输入命令 mongod --repair

3、重启mongodb