MongoDB Simple operation

Source: Internet
Author: User
Tags brew install mongodb bulk insert install mongodb logical operators mongodb

Mongodbubuntu
sudo apt-get install MongoDB
Mac
Brew Install MongoDB
Start
    1. Direct start

sudo mongod

Caveats: Startup failure

  1. Database does not exist create folder/data/db

  2. Insufficient permissions to addsudo

    1. Start parameter description

--portTo set the default port
--dbpathSetting the database path
--configBy specifying the configuration file to start, the configuration mongodb file can be configured by default port, database path and so on

Shut down
    1. Direct kill off (very deprecated, preferably not used)

sudo kill-9 process number
    1. Close directly by entering the database

Use Admindb.shutdownserver ()
Database Display Database list
Show databases//or show DBS
Show current Database
Db
Enter the database
Use database name
Create a database

Create a database automatically whenever you insert data

Deleting a database
Premise use db//Delete database Db.dropdatabase ()
Collection (table) operations Create a collection
    1. The collection is automatically created by default whenever you insert data

    2. Create a collection manually

Db.createcollection (collection name)
Show List of collections
Show collections
Delete Collection
Db. Collection name. Drop ()
Data manipulation creation
Insert data, JSON string Db.my_coll.insert ({"A": "X", "B": {"Q": "A", "P": "B"}, "C": [X-ray]})//BULK INSERT data = [ {"test01": "Val1"}, {"test02": "Val2"}, {"A": {"P": "Q"}, "B": 123}]db.my_coll.insertmany (data)
Update

Default overall update, if you need to use local update, the $set default is to update only once, if you need to add a third parameter batch updatemulti

  Default update is a full update, that is, replace db.stu.update (    //  update Condition     {          "name": "Guo Jing"     },    //  update content     {         "age": 88    })//   Partial update db.stu.update (    //  update conditions     {          "name": "Huang Rong"     },    //  update content      {        //  locally updated content           $set:{             "age":80         }    })//  is updated by default only once, and if a batch update is required, add a third parameter, multi  set to true  is the bulk update db.stu.update (    //  update Condition     {          "Age": {$gt:20}    },    //  update content      {        //  locally updated content           $set:{             "age":36         }    },    //  How to update     {        multi:true    })
Delete
Delete Db.stu.remove (//Delete condition {"Age": 36},//If you want to delete only one {justone:true})//clear the data Db.stu.remov E ({})
Save
Query data via _id If there is an update, insert Db.my_test.save If it does not exist ({"_id": 1, "a": "B"})
Query Simple Query
Db. Collection name. Find ()
Complex queries
  Default query is   and Db.stu.find (    //  query criteria     {          "Hometown": "Mongolia",         "age": 20     })//  comparison operator ($GT, $gte, $lt, $lte, $ne) db.stu.find (    //  query condition     {         "Age": {$gt:20}     })//  logical operators   $ORDB. Stu.find (    //  query criteria     {          "Age": {$gt:20},         $or: [             {"Hometown": "Mongolia"},             {"Gender":false}         ]    })//  range operator   $in, $nin//  represents db.stu.find in the list (     //  Query Conditions     {         "Age": {$nin: [18,45]}    }]/ /  Regular Expression Db.stu.find (    //  query condition     {          "name":/^ yellow/    }) Db.stu.find (    //  query criteria     {         "name": {$regex: "^ yellow"}     })//  custom query Db.stu.find (    //  query criteria     {          $where:  function () {             //  return  true  Show qualifying              // this  indicates the current record              return this.age >= 18 && this.gender == false         }    }) 
Advanced Query
Limit and skip//Note: Skip before limit no matter who first who db.stu.find (). Limit (2) db.stu.find (). Skip (1) db.stu.find (). Limit (2). Skip (1) Db.stu.find (). Skip (1). Limit (2)//Whether the projection shows data//in different database versions in different ways Db.stu.find (//Query condition {},//projection method, display data field {" Name ": 1})//Sort///parameter 1 for ascending order//parameter-1 for descending order Db.stu.find (). Sort ({age:-1, hometown:1})//Count Db.stu.find (). Count ()//Remove duplicates//The first parameter is a deduplication field//The second is a query condition db.stu.distinct (' hometown ', {age:{$gt: 18}})

MongoDB Simple operation

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.