Comparison between mongodb and mysql commands

Source: Internet
Author: User
Tags mysql commands
Here we use the SQL statement in mysql as an example. The C # driver uses samus, which is the first one described above. introduce the MongoDB project. dll creates a Mongo connection to varmongonewMongo (mongodb: localhost); mongo. connect (); obtain a database. If not, a vardbmongo is automatically created. getDatabase

Here we use the SQL statement in mysql as an example. The C # driver uses samus, which is the first one described above. introduce the MongoDB project. dll // create Mongo connection var mongo = new Mongo (mongodb: // localhost); mongo. connect (); // obtain a database. If not, A var db = mongo is automatically created. getDatabase

Here we use the SQL statement in mysql as an example. The C # driver uses samus, which is the first one described above.

Introduce project MongoDB. dll

// Create a Mongo connection
  var mongo = new Mongo("mongodb://localhost");
  mongo.Connect();
// Obtain a database. If no database is created,
  var db = mongo.GetDatabase("movieReviews");
// Create a list and create a document for this list
  var movies = db.GetCollection("movies");

Comparison between mongodb and mysql commands

Traditional relational databases are generally composed of three levels: database, table, and record. MongoDB is composed of databases and collections) and document objects. MongoDB has no concept of columns, rows, and links for tables in relational databases, which reflects the free mode.

MySQL

MongoDB

Description

Mysqld

Mongod

Server daemon

Mysql

Mongo

Client tools

Mysqldump

Mongodump

Logical backup tools

Mysql

Mongorestore

Logical recovery tool

Db. repairDatabase ()

Restore database

Mysqldump

Mongoexport

Data export tool

Source

Export Import

Data Import Tool

Grant * privileges on *. *...

Db. addUser ()

Db. auth ()

Create a user and grant permissions

Show databases

Show dbs

Show library list

Show tables

Show collections

Display table list

Show slave status

Rs. status

Query Master/Slave status

Create table users (a int, B int)

Db. createCollection ("mycoll", {capped: true,

Size: 100000}) and implicitly create a table.

Create a table

Create INDEX idxname ON users (name)

Db. users. ensureIndex ({name: 1 })

Create an index

Create INDEX idxname ON users (name, ts DESC)

Db. users. ensureIndex ({name: 1, ts:-1 })

Create an index

Insert into users values (1, 1)

Db. users. insert ({a: 1, B: 1 })

Insert record

Select a, B from users

Db. users. find ({}, {a: 1, B: 1 })

Query a table

Select * from users

Db. users. find ()

Query a table

Select * from users where age = 33

Db. users. find ({age: 33 })

Conditional Query

Select a, B from users where age = 33

Db. users. find ({age: 33}, {a: 1, B: 1 })

Conditional Query

Select * from users where age <33

Db. users. find ({'age': {$ lt: 33 }})

Conditional Query

Select * from users where age> 33 and age <= 40

Db. users. find ({'age': {$ gt: 33, $ lte: 40 }})

Conditional Query

Select * from users where a = 1 and B = 'Q'

Db. users. find ({a: 1, B: 'q '})

Conditional Query

Select * from users where a = 1 or B = 2

Db. users. find ({$ or: [{a: 1 },{ B: 2}]})

Conditional Query

Select * from users limit 1

Db. users. findOne ()

Conditional Query

Select * from users where name like "% Joe %"

Db. users. find ({name:/Joe /})

Fuzzy search

Select * from users where name like "Joe %"

Db. users. find ({name:/^ Joe /})

Fuzzy search

Select count (1) from users

Db. users. count ()

Obtain the number of table records

Select count (1) from users where age> 30

Db. users. find ({age: {'$ gt': 30}). count ()

Obtain the number of table records

Select DISTINCT last_name from users

Db. users. distinct ('Last _ name ')

Remove duplicate values

Select * from users order by name

Db. users. find (). sort ({name:-1 })

Sort

Select * from users order by name DESC

Db. users. find (). sort ({name:-1 })

Sort

EXPLAIN select * from users where z = 3

Db. users. find ({z: 3}). explain ()

Obtain the storage path

Update users set a = 1 where B = 'Q'

Db. users. update ({B: 'q'}, {$ set: {a: 1 }}, false, true)

Update record

Update users set a = a + 2 where B = 'Q'

Db. users. update ({B: 'q'}, {$ inc: {a: 2 }}, false, true)

Update record

Delete from users where z = "abc"

Db. users. remove ({z: 'abc '})

Delete record

Db. users. remove ()

Delete all records

Drop database if exists test;

Use test

Db. dropDatabase ()

Delete Database

Drop table if exists test;

Db. mytable. drop ()

Delete table/collection

Db. addUser ('test', 'test ')

Add User

ReadOnly --> false

Db. addUser ('test', 'test', true)

Add User

ReadOnly --> true

Db. addUser ("test", "test222 ")

Change Password

Db. system. users. remove ({user: "test "})

Or db. removeUser ('test ')

Delete a user


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.