Database operations:
Show DBS; #查看数据库
Use test; #如果没有就创建一个
DB; #查看当前数据库
Db.dropdatabase (); #删除数据库
Data manipulation:
Show collections; #查看集合
To create a collection, insert:
Create collection; #创建集合
Db.student.insert ({"Name": "Zhang San", "Age": "n", "Sex": "Male", "Class": "Computer 2"}), #如果数据库中不存在集合, create and insert this data
Db.student.insert ({"Name": "John Doe", "Age": "$", "sex": "female", "Phone": "18513081650", "Class": "Computer 1"}); The key-value don't have to be consistent.
Db.student.insert ([{"Name": "Harry", "Age": "the", "Sex": "Male", "Class": "Computer 2 Class"},{"name": "Zhao Liu", "Age": "A", "sex": "female", "phone ":" 18513081650 "," Class ":" Computer 1 Class "}]); #同时插入多条数据
Update:
Db.student.update ({"Name": "Zhang San"},{"name": "Zhang Sanfeng"}); #如果有多条语句, modify only the first one, overwriting the original data
Db.student.update ({"22": "Female"},{"name": "Zhang San Fung"});
Db.student.update ({"Name": "Zhang San"},{$set: {"name": "Zhang Mowgli"}}); #只想改某个key的value使用set
Db.student.update ({"Name": "Harry"},{$set: {"name": "Zhang Mowgli"}},{multi:true}); #把所有的记录都改了
Inquire:
Db.student.find (); #查询全部
Db.student.find ({"Name": "John Doe"}), #查询指定记录, returns the result of this row
Db.student.update ({"Name": "Zhang Sanfeng"},{"name": "Zhang Mowgli", "Age": "", "" Sex ":" Male "});
Db.student.find ({"Name": "Zhang Mowgli", "Age": "}"); #and操作
Db.student.find ({$or: [{"Name": "Mowgli"},{"name": "John Doe"}]}); #or操作
Db.student.find (). Pretty (); #格式化显示
Db.student.find (). Count (); #获取结果的行数
Db.student.find (). Sort ({"Age": -1}); #按照sort里面key的值排序, 1 is positive,-1 is reverse order
Delete:
Db.student.remove (); #删除所有数据
Db.student.remove ({"22": "Female"}); #按照条件删除
Db.student.remove ({"Name": "Zhang Mowgli"},2); #删除几条
Reprint: https://www.cnblogs.com/paopaoquan/p/6375520.html
MongoDB simple additions and deletions to search