Basic Operation commands for MongoDB

Source: Internet
Author: User


= = = MongoDB Basic Operation command =================


DDL Operation ===========================================================================================

Create Schema: Use schema; Create an object in the schema to Db.createcollection ("CollectionName"). Equivalent to create DATABASE DBName;

Create a collection: Db.createcollection ("test"); CREATE TABLE TableName () equivalent in DBMS

Display architecture: Show DBS; Equivalent to the show databases in MySQL;

Number of display sets: Show collections; Show tables

Delete Current schema: use schema;    Db.dropdatabase (); Drop Database DBName

Modify the collection name: Db.  Collectionname.renamecollection ("NewName"); ALTER TABLE oldtable Rename to newtable similar to MySQL;

Delete collection: Db.collections.drop (); Similar to MySQL in drop table tableName;

View your current db:db


User-related Operations ================================================================

Add Users: Db.adduser ("UserName", "PWD")

Verify that the user added success: Db.auth ("UserName", "PWD")

Delete User: Db.removusers ("TestUser")

Permission-Related:

http://blog.itpub.net/22664653/viewspace-715617/


DML Operation =====================================================================

Query collection: DB. Collectionname.find ({}); Equivalent to select * from TableName

Equality condition query: db. Collectionname.find ({key01:value01}); Equivalent to select * from TableName where key01=value01

Different values for query fields (DE-weight): db. Collectionname.distinct ("key") similar to select DISTINCT (key) from table

Fuzzy query: Db.collections.find ({key:/ang/}); SELECT * from TableName where key like '%ang% ';

:d b.collections.find ({key:/^ang/}); SELECT * from TableName where key like ' ang% ';

:d b.collections.find ({key:/g$/}); SELECT * from TableName where key like '%ang '

Query out the specified list: Db.collections.find ({},{key01:false,key02:true,key03:true}); Select key02,key03 from TableName

Range Lookup: Db.collections.find ({key01:{$gt: 20}}); SELECT * from TableName where key01 > 20

:d B.collections.find ({key01:{$lge: 20}}); SELECT * from TableName where key01 >= 20

:d B.collections.find ({key01:{$lt: 20}}); SELECT * from TableName where Key01 < 20

:d B.collections.find () {key01:{$lte: 20}}; SELECT * from TableName where key01 <= 20

Sort Ascending: Db.collections.find ({}). sort ({key:1}); SELECT * from TableName ORDER by key ASC

Sort Descending: Db.collections.find ({}). sort ({key:-1}); SELECT * from TableName ORDER BY key Desc

Record Rollup: Db.collections.find ({}). Count ({}); Select COUNT (*) from TableName

:d b.collections.find ({}). count ({key:false}) Select COUNT (key) from TableName

Returns the specified number of rows: Dbquery.shellbatchsize=num/db.collections.find ({}). Limit (num) similar to SQL Server SET ROWCOUNT num

Operation of OR: Db.collections.find ({"$or": [{KEY01:VALU01},{KEY02:VALUE02}]}); SELECT * from TableName where key01=valu01 or KEY02=VALUE02

In operation: Db.test.find ({key01:{$in: [valu01,valu02,valu03]}}); SELECT * from TableName where key01 in (valu01,valu02,valu03)

Operation of and: Db.collections.find ({key01:{$gte: $, $lte: 28}}); SELECT * from TableName where key01 >=24 and Key01 <= 28

:d b.collections.find ({key01:valu02,key02:valu02}); SELECT * from TableName where key01=valu01 and key02=valu02

Not equal to Operation: Db.collections.find ({key01:{$ne: valu01}}); SELECT * from TableName where key01 <> valu01

$not Related actions: Db.collections.find ({key:{$not: {$in: [valu01,valu02]}}}); SELECT * from TableName where key not in (VALU01,VALU02)

Note: Db.collections.find (key:{$not: valu01}); No such wording.

Regular Expressions: Follow-up studies

Related Operations for arrays

$mod: Modulo function

$inc

$maxscan: Make the maximum number of scanned documents in this query

$min: The start condition of the query,

$max: End Condition for query

$SHOWDISKLOC: Displays the location of the result on the disk.

Delete data: db. Collectionname.remove ({key:value});

Modified data: Db.collections.update ({key01:valu01}},{"$set": {Key02:newvalue}});

Add data: db. Collectionname.save ({key01:value01,key02:value02})/db. Collectionname.insert ({KEY01:VALUE01,KEY02:VALUE02})


View Help: Db. Collectionname.help ();




This article is from the "SQL Server MySQL" blog, so be sure to keep this source http://dwchaoyue.blog.51cto.com/2826417/1602588

Basic Operation commands for 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.