MongoDB Basic Use Tutorial

Source: Internet
Author: User
Tags numeric mongodb mongodb query mongodb server sort
MongoDB Basic Use tutorial

Basic Command Use

MONGO default port
27017

//default storage use path
/data/db//need to create manually, and give read/write permissions

//start MongoDB server
Mongod--config/usr/ local/etc/mongod.conf

//link MONGO command
MONGO 

//View all data
show DBS

//DB currently in use

Create a database

Use database_name
//Do not exist to create, otherwise switch to the database

Deleting a database

Db.dropdatabase ()  
//Deletes the currently used database

Delete Collection

Delete Collection
Db.collection_name.drop ()//collection_name the name of the collection you want to delete

Update document

MongoDB uses the update () and Save () methods to update the documents in the collection.

The update () method is used to update a document that already exists

Db.collection.update (
   <query>,
   <update>,
   {
     upsert: <boolean>,
     Multi: <boolean>,
     writeconcern: <document>
   }
)

* Query:update Query conditions
* Update:update objects and some updated operators (e.g.,, Inc ...
* Upsert: Optional, this parameter means that if there is no record of update, insert objnew,true as INSERT, default is False, do not insert.
* Multi: Optional, mongodb default is False, only update the first record found, if this parameter is true, will be found on the condition of a number of records update all.
* Writeconcern: Optional, throws an exception level.

    Example: Update student name to Loukit to Allen

    db.student.update ({' name ': ' Loukit '},{$set: {' name ': ' Allen '}})

The Save () method replaces an existing document with an incoming document

Db.collection.save (
   <document>,
   {
     writeconcern: <document>
   }
)

* Document: Documentation data.
* Writeconcern: Optional, throws an exception level.

    Modify the _id to 58eb301cfabd4826d05ecd1d document data, the fields that are not written will disappear
    Db.student.save ({"_id": ObjectId ("58eb301cfabd4826d05ecd1d") ), "name": "LK",  "id": "+", "date": "2017-08-01"})

Delete a document

Use the Remove () function in MongoDB to remove data from the collection

Db.collection.remove (
   <query>,
   {
     justone: <boolean>,
     Writeconcern: <document>
   }
)

* Query: (optional) The condition of the deleted document.
* Justone: (optional) If set to TRUE or 1, only one document is deleted, false by default.
* Writeconcern: (optional) the level at which the exception is thrown.

    Delete Only one
    db.student.remove ({' name ': ' Harley '},{justone:true}); 

Querying documents

The syntax format for MongoDB query data is as follows

Db. Collection_name.find ()

The Find () method displays all documents in an unstructured manner, which is not very readable, and you can use the pretty () method:

Db. Collection_name.find (). Pretty ()

Conditional query

and Conditions

MongoDB's Find () method can pass in multiple keys (key) and each key (key) is separated by commas for conditional query

Db.col.find ({key1:value1, key2:value2}). Pretty ()

//query for the data with the name LK and ID 4
db.student.find ({' name ': ' LK ', ' Id ': ' 4 '})

OR condition

The MongoDB OR conditional statement uses the keyword $or in the following syntax format:

Db.col.find (
   {
      $or: [
         {key1:value1}, {key2:value2}
      ]
   }
). Pretty ()

// The query name is LK or the data with ID 4
db.student.find ({$or: [{' Name ': ' LK '},{' id ': ' 4 '}]}). Pretty ()

Query operation symbols in MongoDB

operator Syntax Format Example
Equals {Key:value} Db.student.find ({"Age": 20})
Less than {key:{$lt: value}} Db.student.find ({"Age": {$lt: 20}})
Greater than {key:{$gt: value}} Db.student.find ({"Age": {$gt: 20}})
Less than or equal to {key:{$lte: value}} Db.student.find ({"Age": {$lte: 20}})
Greater than or equal to {key:{$gte: value}} Db.student.find ({"Age": {$gte: 20}})
Not equal to {key:{$ne: value}} Db.student.find ({"Age": {$ne: 20}})
Limit () and Skip ()

Limit: Specifies the number of data records that MongoDB reads, which accepts a numeric parameter

Db. Collection_name.find (). Limit (number)

Skip: Skips a specified number of data, which takes a numeric parameter as the number of skipped record bars

Db. Collection_name.find (). Limit (number). Skip (number)
Sorting sort ()

The sort () method specifies the sorted field by parameter and uses 1 and-one to specify how the sort is sorted, where 1 is in ascending order, and 1 is used in descending order

Db. Collection_name.find (). Sort ({key:1})
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.