MongoDB数据库笔记记录

1,数据库的安装版本

32位,64位,32位最大只能存储2G的数据,64位无限制,版本(偶数为稳定版,奇数为开发版)

2,数据库安装CMD命令

(windows开机自启动MongoDB服务,假定在D盘创建MongoDB文件夹以及相应目录2.1—>D:\MongoDB\bin,2.2—>D:\MongoDB\data\db,2.3—>D:\MongoDB\logs):
第一步:mongod –dbpath D:\MongoDB\data\db –logpath D:\MongoDB\logs\MongoDB.log –serviceName MongoDB –install
第二步:net start MongoDB
注:如果不是开机自启动,每次使用数据库,都需要手动的打开MongDB服务

3,数据库操作

(命令行命令,假定数据库名=MyMongoDB,集合名=MyCollection):
show dbs:查看所有数据库
use MyMongoDB:切换当前数据库
show collections: 查看当前数据库的所有集合(表)
db.MyCollection.find():查看当前数据库的MyCollection集合的所有记录
show users:查看用户
db.help():查看数据库操作命令帮助
db.MyCollection.help():查看集合操作命令帮助
db.MyCollection.find().help():查看find命令的帮助
rs.help():~?
db.dropDatabase():删除当前使用数据库
db.cloneDatabase(“127.0.0.1”): 将指定机器上的数据库的数据克隆到当前数据库
db.copyDatabase(“MyCollection”, “temp”, “127.0.0.1”):将本机的MyCollection的数据复制到temp数据库中
db.repairDatabase():修复当前数据库
db.getName():查看当前使用的数据库
db.stats():显示当前db状态
db.version():当前db版本
db.getMongo():查看当前db的链接机器地址
Collection聚集集合
db.createCollection(“MyCollection”, {size: 20, capped: 5, max: 100}):创建一个聚集集合(table)
db.getCollection(“MyCollection”):得到指定名称的聚集集合(table)
db.getCollectionNames():得到当前db的所有聚集集合
db.printCollectionStats():显示当前db所有聚集索引的状态
db.addUser(“name”):添加一个用户
db.addUser(“userName”, “pwd123”, true); 添加用户、设置密码、是否只读
db.auth(“userName”, “123123”):~?
show users:显示当前所有用户
db.removeUser(“userName”):删除用户
db.getPrevError():查询之前的错误信息~?
db.resetError():清除错误记录~?
db.MyCollection.count():查询当前集合的数据条数
db.MyCollection.dataSize():查看数据空间大小
db.MyCollection.getDB():得到当前集合所在的db
db.MyCollection.stats():得到当前集合的状态
db.MyCollection.totalSize():得到集合总大小
db.MyCollection.storageSize():集合储存空间大小
db.MyCollection.getShardVersion():Shard版本信息
db.MyCollection.renameCollection(“MyColl”):将MyCollection重命名为MyColl
db.MyCollection.drop():删除集合
db.MyCollection.distinct(“name”):会过滤掉name中的相同数据,查询去掉后的当前聚集集合中的某列的重复数据
db.MyCollection.find({“age”: 22}):查询age = 22的记录
db.MyCollection.find({age: {$gt: 22}}):查询age > 22的记录
db.MyCollection.find({age: {$lt: 22}}):查询age < 22的记录
db.MyCollection.find({age: {$gte: 25}}):查询age >= 25的记录
db.MyCollection.find({age: {$lte: 25}}):查询age <= 25的记录
db.MyCollection.find({age: { gte:23, lte: 26}}):查询age >= 23 并且 age <= 26
db.MyCollection.find({name: /mongo/}):查询name中包含 mongo的数据
db.MyCollection.find({name: /^mongo/}):查询name中以mongo开头的
db.MyCollection.find({}, {name: 1, age: 1}):查询指定列name、age数据
db.MyCollection.find({age: {$gt: 25}}, {name: 1, age: 1}):查询指定列name、age数据, age > 25
db.MyCollection.find().sort({age: 1}):按照列age升序排序
db.MyCollection.find().sort({age: -1}):按照列age降序排序
db.MyCollection.find().limit(5): 查询前5条数据
db.MyCollection.find().skip(10):查询10条以后的数据
db.MyCollection.find().limit(10).skip(5):查询在5-10之间的数据
db.MyCollection.find({$or: [{age: 22}, {age: 25}]}):查询age=22或者age=25的数据
db.MyCollection.findOne():查询第一条数据
db.MyCollection.find({age: {$gte: 25}}).count():查询age>=20记录条数
db.MyCollection.find({sex: {$exists: true}}).count():按照某列进行排序
db.MyCollection.ensureIndex({name: 1}):创建索引~?
db.MyCollection.ensureIndex({name: 1, ts: -1})
db.MyCollection.getIndexes():查询当前聚集集合所有索引
db.MyCollection.totalIndexSize():查看总索引记录大小
db.MyCollection.reIndex():读取当前集合的所有index信息
db.MyCollection.dropIndex(“name_1”):删除指定索引
db.MyCollection.dropIndexes():删除所有索引索引
db.MyCollection.update({age: 25}, {$set: {name: ‘changeName’}}, false, true):更新age=25的文档的name键值为=changeName
db.users.update({name: ‘FairyTailQ’}, {$inc: {age: 50}}, false, true):更新name=FairyTailQ的文档的age键值=age+50
db.users.remove({age: 132}):删除

db.MyCollection.findAndModify({
    query: {age: {$gte: 25}}, 
    sort: {age: -1}, 
    update: {$set: {name: 'a2'}, $inc: {age: 2}},
    remove: true
});
db.runCommand({ findandmodify : "MyCollection", 
    query: {age: {$gte: 25}}, 
    sort: {age: -1}, 
    update: {$set: {name: 'a2'}, $inc: {age: 2}},
    remove: true
});

updateremove 其中一个是必须的参数; 其他参数可选。
query:查询过滤条件
sort:如果多个文档符合查询过滤条件,将以该参数指定的排列方式选择出排在首位的对象,该对象将被操作
remove:若为true,被选中对象将在返回前被删除
print(“Hello World!”):打印Hello World
tojson(new Object());tojson(new Object(‘a’));:将一个对象转换成json
> for (var i = 0; i < 30; i++) {
… db.MyCollection.save({name: “u_” + i, age: 22 + i, sex: i % 2});
… }循环添加了30条数据
>var cursor = db.MyCollection.find();
> while (cursor.hasNext()) {
printjson(cursor.next());
}:find 游标查询,查询所有的users信息
var cursor = db.MyCollection.find();
while (cursor.hasNext()) { printjson(cursor.next); }:find 游标查询,查询所有的users信息
db.MyCollection.find().forEach(printjson):forEach中必须传递一个函数来处理每条迭代的数据信息
var cursor = db.users.find();
cursor[4];取得下标索引为4的那条数据,将find游标当数组处理
cursor.length()或者cursor.count()既然可以当做数组处理,那么就可以获得它的长度:
for (var i = 0, len = c.length(); i < len; i++) printjson(c[i]):那样我们也可以用循环显示数据
> var arr = db.MyCollection.find().toArray();
> printjson(arr[2]);用toArray方法将其转换为数组,将find游标转换成数组
db.MyCollection.find({age: {$lte: 28}}, {age: 1}).forEach(printjson)
db.MyCollection.find({age: {$lte: 28}}, {age: true}).forEach(printjson):只显示age <= 28的并且只显示age这列数据
db.MyCollection.find({age: {$lte: 28}}, {age: false}).forEach(printjson):排除age的列
db.MyCollection.find({x:4}).forEach(function(x) {print(tojson(x));}):forEach传递函数显示信息

4,数据库断电崩溃后修复

创建.bat文件,内容如下:(假定数据库安装在D:MongoDB)

echo off
D:
echo "进入D盘"
cd MongoDB\data\db
echo "进入数据库目录"
del mongod.lock
echo "删除lock文件"
cd ..
cd ..
cd bin
mongod --remove --serviceName MongoDB
echo "删除数据库服务"
mongod --dbpath D:\MongoDB\data\db --logpath D:\MongoDB\logs\MongoDB.log --serviceName MongoDB --install
echo "重建数据库服务"
net start MongoDB
echo "启动服务"
pause
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值