一、基础命令
use test–选库
显示 switched to db test
db.createCollection(‘goods’)
显示(‘ok’:1)
show dbs
显示当前所有的库
use shop
switched to db shop
db.goods.insert({name:‘zhangsan’,age:9})
显示writeResult({‘nInserted’:1})
show dbs–显示当前所有的库
2,查看数据库下面的集合
use shop
switched to db shop
show collections
goods
show tables
goods
3,删除数据和删除集合
use test
switched to db test
db.dropDatabase()
(“dropped”:“test”,“ok”:1)
use shop
switched to db shop
db.goods.drop()
true
二、CRUD命令
1、插入:insert
单条插入
db.stu.insert({name:‘lisi’,age:10,gender:1})
显示 writeResult({“nInserted”:1})–成功 0则失败 _id字段自动创建,主键自动设置到这个字段 这个字段可手动指定,但不可重复
db.stu.find()查看
显示 _id:Obj(一堆乱码),name:lisi,age:10,gender:1
多条插入
db.stu.insert({文档},{文档},{文档}…)
查找 find
语法:db.stu.find(查询表达式,查询的列)
db.stu.find({},{name:1,_id:0})—0不显示 1显示
查询表达式:条件过滤
Sql
select*from stu
Where 查询表达式
关系:
(1)等于:
Key=value–>{name:‘lisi’}–{key:value}
查询年龄为11的人
db.stu.find({age:11},{age:1,_id:0,name:1})
(2)不等关系
大于:>–KaTeX parse error: Expected '}', got 'EOF' at end of input: gt-->{key:{ gt:value}}
年龄大于20
db.stu.find({age:{ KaTeX parse error: Expected 'EOF', got '}' at position 6: gt:20}̲},{_id:0,name:1…gte–>{ KaTeX parse error: Expected 'EOF', got '}' at position 10: gte:value}̲ 小于:<---->lt—>{ KaTeX parse error: Expected 'EOF', got '}' at position 9: lt:value}̲ 年龄小于20岁的 d…lt:20}},{_id:0,name:1,age:1})
小于等于:<=—KaTeX parse error: Expected '}', got 'EOF' at end of input: lte-->key:{ lte:value}
不等于:<>—KaTeX parse error: Expected '}', got 'EOF' at end of input: ne--->key:{ ne:value}
在条件查询中的and,多个条件
Where age=10 and name=–>{age:10,name:‘lisi’}
这种and操作不能作范围查询,只能做等值查询
db.stu.find(age:{ KaTeX parse error: Expected 'EOF', got '}' at position 6: gt:10}̲,age:{ lt:

这篇博客介绍了MongoDB的基础命令,包括选库、创建集合、查看数据库及集合、数据的插入、删除、更新和查找,以及聚合操作。还涉及到索引的创建、删除以及数据的导入导出。此外,文章提到了MongoDB的复制集配置和验证。
最低0.47元/天 解锁文章
234

被折叠的 条评论
为什么被折叠?



