MongoDB Basic Operations

Source: Internet
Author: User

1. Enter the MONGO environment

Mongo

 [[email protected] ~]# mongomongodb shell version v3.4.16connecting To:mongodb://127.0.0.1:27017mongodb Server Version:3.4.16server has startup warnings: 2018-07-20t17:53:38.109+0800 I CONTROL  [initandlisten]& nbsp;2018-07-20t17:53:38.109+0800 I Control  [initandlisten] * * warning:access CONTROL is not enabled for the Databa se.2018-07-20t17:53:38.109+0800 I CONTROL  [initandlisten] * *          read and write access To data and configuration is unrestricted.2018-07-20t17:53:38.109+0800 I CONTROL  [initandlisten]  2018-07-20t17:53:38.109+0800 I control  [initandlisten] 2018-07-20t17:53:38.109+0800 I control  [ Initandlisten] * * WARNING:/sys/kernel/mm/transparent_hugepage/enabled is ' always '. 2018-07-20t17:53:38.109+0800 I CONTROL  [initandlisten] * *        WE suggest setting it to ' Never ' 2018-07-20t17:53:38.109+0800 I CONTROL  [initandlisten] 2018-07-20t17:53:38.109+0800 I CONTROL  [initandlisten] * * WARNING:/sys/kernel/mm/transparent_hugepage/defrag is ' always '. 2018-07-20t17 : 53:38.109+0800 I CONTROL  [initandlisten] * *        WE suggest setting it to ' Never ' 2018-07-20t1 7:53:38.109+0800 I CONTROL  [initandlisten] >2. Querying all databases

Show DBS;

> Show DBS Admin 0.000GBlocal 0.000GB 3. Create or switch libraries (If the database does not exist, create the database, or switch to the specified database)

Use MyDB;

> Use mydbswitched to DB mydb> show Dbsadmin 0.000GBlocal 0.000GB

mydb Not in the list of databases, to show it, we need to   mydb The database inserts some data.

4. Create a Collection

> db.createcollection ("Log") {"OK": 1}> show dbsadmin 0.000GBlocal 0.000GBmydb 0.000GB

5. Insert data (if the collection does not exist at the time of insertion, including creating a collection and inserting data two actions) and update

> Db.logdata.insert ({name: ' Zhangsan ', Age: ' 25 '}); Writeresult ({"ninserted": 1} > Db.logdata.insert ({' _id ': ' Terrywang ', ' super_admin ': true}) Writeresult ({"Ninserte D ": 1}) > Db.logdata.find ({' _id ': ' Terrywang '}) {" _id ":" Terrywang "," Super_admin ": true} > Db.logdata.update ({' _id ': ' Terrywang '}, {$set: {' super_admin ': false}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) > D B.logdata.find ({"_id": "Terrywang"}) {"_id": "Terrywang", "Super_admin": false}

6. View Collection Contents

> Db.logdata.find () {"_id": ObjectId ("5b529ef8bfde59a50f5e5397"), "name": "Lisi", "age": "{"} {"_id": "Terrywang" , "Super_admin": false}> Db.logdata.findOne ({' _id ': ' Terrywang '}) {"_id": "Terrywang", "Super_admin": true}

7. Filter by criteria

> Db.logdata.find ({"Name": "Zhangsan"}); {"_id": ObjectId ("5b529e14bfde59a50f5e5396"), "name": "Zhangsan", "Age": "25"}

Regular lookup

> Db.logdata.find ({"_id":/e*/i}) {"_id": "Terrywang", "Super_admin": false}

I indicates whether it is case-insensitive, and I is ignoring case

Filter to only one column

> Db.logdata.find ({},{d:1}) {"_id": ObjectId ("5b529ad3aea13d710cc622d5")}

8. Deleting a document

Insert a document first

> Db.logdata.insert ({name: ' Lisi ', Age: ' 30 '}); Writeresult ({"ninserted": 1}) > Db.logdata.find () {"_id": ObjectId ("5b529e14bfde59a50f5e5396"), "name": "Zhangsan" , "Age": "+"} {"_id": ObjectId ("5b529ef8bfde59a50f5e5397"), "name": "Lisi", "Age": "30"}

Delete a document

> Db.logdata.remove ({"Name": "Zhangsan"})

Writeresult ({"nremoved": 1}) > Db.logdata.find () {"_id": ObjectId ("5b529ef8bfde59a50f5e5397"), "name": "Lisi", "age ":" 30 "}

9. Display Collection

> Show Tables;loglogdata

10. Display the number of documents in the collection

> Db.logdata.find () {"_id": ObjectId ("5b529ef8bfde59a50f5e5397"), "name": "Lisi", "age": "{"} {"_id": "Terrywang" , "Super_admin": false}> Db.logdata.count () 2

11. View the collection index

> Db.logdata.getIndex () 2018-07-21t11:42:09.337+0800 E QUERY [Thread1] TypeError:db.logdata.getIndex is not a functi                        On: @ (Shell):1:1> db.logdata.getIndexes () [{"V": 2, "key": { "_id": 1}, "name": "_id_", "ns": "Mydb.logdata"}]

12. Delete a table

> Db.logdata.drop () true> show tables;logtest> Db.test.drop () true> show Tableslog

MongoDB Basic Operations

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.