View help
Help//会有各种命令提示
Show database
show dbs
Create database name
use dbname// 如果数据库不存在,则创建数据库dbname,否则切换到指定数据库dbname。创建的数据库并不在数据库的列表中,要显示它,我们需要向数据库dbname插入一些数据
The default database in MongoDB is test. If you do not create a new database, the collection will be stored in the test database.
Display collections in the database
show collections//显示的是创建的所有数据库的名称
Increase data
db.数据库名称.save()
Case study
db.web.save({"name":"老李"}) 创建了名为web的集合,并新增了一条{"name":"老李"} 的数据
db.web.insert({"name":"ghost", "age":10}) 在web集合中插入一条新数据,如果没有web这个集合,mongodb会自动创建
The difference between save() and insert()
If the newly added data primary key already exists, insert() will not do any operation and prompt an error, while save() will change the original content to the new content.
delete data
db.users.remove({}) 删除users集合下所有数据
db.users.remove({"name": "lecaf"}) 删除users集合下name=”lecaf”的数据
db.users.drop()或db.runCommand({"drop":"users"}) 删除集合users
db.runCommand({"dropDatabase": 1}) 删除当前数据库,注意 此处的1没加双引号
Find data
db.users.find() 查找users集合中所有数据
db.users.findOne() 查找users集合中的第一条数据
db.users.find().pretty() 格式化查询到的数据
change the data
db.数据库名称.update({})
Case study
db.class0.update({name:'阿红'},{$set:{age:24}})
db.web.update({"name":"a1"}, {$set: {sex:”women”}},true,true) 修改name=a1的数据为sex=1,第一个参数是查找条件,第二个参数是修改内容,主键不能修改,
Choose or create a database
use 数据库名称
Insert document or data
db.集合名称.save(变量);
Case study
Query collection data
db.集合名称.find()
Case study