MongoDB some simple additions and deletions to change the statement

Source: Internet
Author: User

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); #删除几条

MongoDB some simple additions and deletions to change the statement

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.