Basic operation of MongoDB

Source: Internet
Author: User
Tags mongodb limit

Querying all the Databases

Show DBS

Specify to a database/or create

Use database name

Specify storage size

Db.createcollection ("Mycoll", {capped:true,size 10000})

Inserting data

Db. Collection. Insert ({key:values})

Db. Collection. Save ({key:values})

Querying documents

Db. Collection. Find ()

Total query data

Db. Collection. Count ()

MongoDB Limit () method

If you need to read a specified number of data records in MongoDB, you can use the MongoDB limit method, and the limit () method accepts a numeric parameter that specifies the number of record bars read from MongoDB.

Specify the number of bars to read

Db. Set find (). limit (count);

Skips a specified number of rows

Db. Collection. Find (). Limit (count). Skip (count) skip defaults to 0

Sort () method ordering

Parameters are two 1 or-1, ascending or descending

Db. Set find (). Sort ()

Deleting a database

Use database

Dorp. Database ()

Delete Collection

Db.collection.drop ();

Querying database Collections

Db. Collection. Find ()

Update document

Update ()

123456789 db.collection. update ( &NBSP;&NBSP;&NBSP; < Query>, &NBSP;&NBSP;&NBSP; < update >,      { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP; Code class= "SQL Plain" >upsert: <boolean>,       multi: <boolean>,        writeconcern: <document>      } )

 

Parameter description:

    • Query: The search condition for update, similar to where in SQL update query.
    • Update: Update object and some updated operators (such as $, $inc ... ) can also be understood as the SQL update query within the set after the
    • 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

Db. Collection. Update ({Query condition}, ($set: {Modify content}))

If you want to modify more than one document

Db. Collection. Update ({Query condition}, ($set: {Modify content}), {multi:true})

Eg:db.mylog.update ({"Name": "Zhangsan"}, {$set: {"test": "Test1"}})

Delete a document

Db. Collection. Remove (delete condition)

Delete the first data found

Db. Collection. Remove (delete condition)

Delete all

Db. Collection. Remove ({})

Multi-Key Query

Eg:db. Collection. Find ({key:values,key:values}). Pretty ()//equivalent to the Where condition one and condition two in SQL

Or

Db. Collection. Find (

{

$or [{key:values},

{Key:values}

]

}

). Pretty ()

With AND and OR

Db. Collection. Find ({key:values}, $or [{condition two},{or condition two}]. Pretty ()

MongoDB Index

Db. Collection. Ensureindex ({indexed by what})

Eg:db.numbers.ensureIndex ({num:1})

Explain usage:

Eg:db.numbers.find ({num:{"$gt": 19995}}). explain

Ext.: http://www.cnblogs.com/tsxylhs/p/5657298.html

Basic operation of MongoDB

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.