mongodb基础1

​启动服务1.mongod --dbpath=..\data\db2.创建mongod.confstorage: dbPath: 绝对路径...\data命令:mongod -f ../conf/mongod.conf 或 mongod --config ../conf/mongod.conf启动控制台bin目录下:mongo --host=127.0.0.1一些命令选择/创建数据库:use 数据库名称查看数据库:show dbs / show da
摘要由CSDN通过智能技术生成


启动服务

1.mongod --dbpath=..\data\db
2.创建mongod.conf

storage:
  dbPath: 绝对路径...\data

命令:mongod -f ../conf/mongod.confmongod --config ../conf/mongod.conf

启动控制台
bin目录下:
mongo --host=127.0.0.1


一些命令

选择/创建数据库:
use 数据库名称
查看数据库:
show dbs / show databases
查看当前正在使用的数据库:
db
删除数据库:
db.dropDatabases()

** 集合操作**

查看
show collections

创建
集合类似关系型数据库的表,可显式创建也可隐式创建

显式创建:直接创建
命令(name:创建的集合名称):
db.createCollection("name")

隐式创建:插入文档的时候,如果没有这个集合,就自动创建一个

删除
db.collection.drop()db.集合.drop()


文档基本CURD

文档的数据结构和json基本一样,所有储存在集合中的数据都是BSON格式
单个文档的插入
使用insert()或save()
语法:

db.collection.insert(
	<document or array of documents>,
	{
	  writeConcern: <document>,
	  ordered: <boolean>
	}
}
ParameterTypeDescription
documentdocument or array要插入到集合中的文档或文档数组。(json格式)
writeConcerndocument性能损耗性的级别,了解就好
orderedboolean可选,如果为真,按顺序插入数组中的文档,如果其中一个出错,将返回而不处理数组中的其余文档。如果为假,执行无需插入,如果出错,急需处理数组中的主文档。认为true

举例:

db.comment.insert(
	{
		"articleid":"1000",
		"content":"good day",
		"userid":"1001",
		"nickname":"haozi",
		"createdatetime":new Date(),
		"likenum":NumberInt(10),
		"state":null
	}
)

说明:

  1. 如果comment集合不存在,就会隐式创建
  2. mongo中的数字,默认是double类型,如果要整型,必须用函数NumberInt(整形数字),否则取出来就会有问题。
  3. 插入当前日期使用new Date()
  4. 插入的数据没有指定_id,会自动生成主键
  5. 如果某字段没值,可以赋值为null或不写该字段

批量插入

db.collection.insertMany(
	[ <document1> , <document2> , ...],
	{
	  writeConcern: <document>,
	  ordered: <boolean>
	}
}

举例:

db.comment.insertMany([
	{
		"_id":"1",
		"articleid":"1001",
		"content":"good day",
		"userid":"1001",
		"nickname":"haozi",
		"createdatetime":new Date(),
		"likenum":NumberInt(1),
		"state":null
	},
	{
		"_id":"2",
		"articleid":"1002",
		"content":"hello world",
		"userid":"1002",
		"nickname":"haozi",
		"createdatetime":new Date(),
		"likenum":NumberInt(2),
		"state":null
	}
])


语法:

db.collection.find(<query> , [projection])
ParameterTypeDescription
querydocument可选。使用查询运算符指定选择筛选器。若要返回集合中的所有文档,省略此参数或传递{}
projectiondocument可选。指定要在与查询筛选器匹配的文档中返回的字段(投影)

查询所有
db.comment.find()db.comment.find({})

条件查询
db.comment.find({userid:"1001"})

查询一条
多条的时候只要第一条:
db.comment.findOne({userid:"1001"})

投影查询
如果只想要查询结果中的部分字段,而不是所有字段,就用这个。

db.comment.find(
	{userid:"1001"},
	{userid:1,_id:0}
)

1表示显示,0表示不显示。查询出来的都会把_id主键带上,所以如果不想显示出来,就写0

try-catch
批量插入如果插入失败,会终止插入,但已经插入成功的数据不会回滚,所以批量的时候会有风险,因此可以用try-catch进行异常捕捉。

try{
	db.comment.insertMany([
		{
			"_id":"1",
			"articleid":"1001",
			"content":"good day",
			"userid":"1001",
			"nickname":"haozi",
			"createdatetime":new Date(),
			"likenum":NumberInt(1),
			"state":null
		},
		{
			"_id":"2",
			"articleid":"1002",
			"content":"hello world",
			"userid":"1002",
			"nickname":"haozi",
			"createdatetime":new Date(),
			"likenum":NumberInt(2),
			"state":null
		}
	])
}catch (e){
	print(e);
}

db.collection.update(query, update, options)
//或
db.collection.update(
	<query>,
	<update>,
	{
		upsert: <boolean>,
		multi: <boolean>,
		writeConcern: <document>,
		collation: <document>,
		arrayFilters: [<filterdocument1>, ...],
		hint: <document|string>
	}
)
ParameterTypeDescription
documentdocument or array要插入到集合中的文档或文档数组。(json格式)
writeConcerndocument性能损耗性的级别,了解就好
orderedboolean可选,如果为真,按顺序插入数组中的文档,如果其中一个出错,将返回而不处理数组中的其余文档。如果为假,执行无需插入,如果出错,急需处理数组中的主文档。认为true

覆盖修改
执行覆盖修改后,除修改字段外其他字段都不见了

db.comment.update(
	{_id:"2"},
	{likenum:NumberInt(1006)}
)

结果:
执行前:
执行前
执行后:
执行后
局部修改
为了解决覆盖修改的问题,需要使用修改器$set

db.comment.update(
	{_id:"2"},
	{
		$set:{
			likenum:NumberInt(666)
		}
	}
)

批量修改
注意,上面两种如果有多个符合条件的,只会修改第一条,如果想批量修改,需要加{multi:true}

db.comment.update(
	{_id:"2"},
	{
		$set:{
			likenum:NumberInt(666)
		}
	},
	{multi:true}
)

值自动增长
如果想针对某个值在原有值基础上新增或减少,关键词是$inc

db.comment.update(
	{_id:"2"},
	{
		$inc:{
			likenum:NumberInt(1)
		}
	}
)


语法

db.集合名称.remove(条件)

举例

db.comment.remove({_id:"1"})
//全部删除传空就好,慎用
db.comment.remove({})

查询进阶

统计查询
语法:

db.collection.count(query,options)
ParamterTypeDescription
querydocument查询选择条件
optionsdocument可选。用于修改计数的额外选项。
  1. 统计所有记录数
db.collection.count()
  1. 条件查询
db.collection.count({userid:"1001"})

分页查询
关键词:
limit():读取指定数量的数据。如果不写,默认20
skip():跳过指定数量的数据。如果不写,默认0

db.comment.find().limit(2).skip(0)

排序查询
关键词:
sort():1:升序、-1:降序

db.comment.find().sort({userid:-1,likenum:1})

注意:skipt()、limit()、sort()三个执行顺序是sort、skip、limit。和编写顺序无关。

模糊查询(正则查询)
mongodb的模糊查询是通过正则表达式的方式实现的
格式:

db.collection.find({field:/正则表达式/})

注意:正则是js的语法

//查询包含关键词的
db.comment.find({content:/good/})
//查询以某个关键词开头的
db.comment.find({content:/^world/})

比较查询
就是<,<=,>,>=

db.collection.find({field:{$gt : value}})		//field > value
db.collection.find({field:{$lt : value}})		//field < value
db.collection.find({field:{$gte : value}})		//field >= value
db.collection.find({field:{$lte : value}})		//field <= value
db.collection.find({field:{$ne : value}})		//field != value

包含查询
关键词:
$in:包含
$nin:不包含

//包含
db.collection.find(
	{
		userid:{$in:["1001","1002"]}
	}
)
//不包含
db.collection.find(
	{
		userid:{$nin:["1001","1002"]}
	}
)

条件连接查询
查询条件有两个以上。关键词:
$and:与。类似sql中的and
$or:或。类似sql中的or
格式:
$and:[{},{},{}]
$or:[{},{},{}]

db.comment.find(
	{
		$and:[
			{likenum:{$gte:NumberInt(2)}},
			{likenum:{$lt:NumberInt(4)}}
		]
	}
)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值