To learn about a database, you must first learn how to connect to the database and how to perform basic addition, deletion, modification, and query. Other advanced functions can be learned. The following describes the basic addition, deletion, modification, and query of mogodb. 1. Create a database: usemkyongdb (If yes, jump to this database. If no, create and redirect). All databases are displayed: showdbs2 and create a set (
To learn about a database, you must first learn how to connect to the database and how to perform basic addition, deletion, modification, and query. Other advanced functions can be learned. The following describes the basic addition, deletion, modification, and query of mogodb. 1. Create a database: use mkyongdb (If yes, jump to this database. If no, create and redirect). All databases are displayed: show dbs 2. Create a set (
To learn about a database, you must first learn how to connect to the database and how to perform basic addition, deletion, modification, and query. Other advanced functions can be learned.
The following describes how to add, delete, modify, and query mogodb.
1. Create a database: use mkyongdb (if any, jump to this database. If not, create and jump)
Show all databases: show dbs
2. Create a set (equivalent to a relational database table ):
Set operation:
(1) Insert data
Db. users. insert ({'name': 'xumingxiang ', 'sex': 'man'}) to create a set of users
Display All collections in the current database: show collections
Show all data documents in the users set: db. users. find ()
Display the number of records in the Set: db. users. count ()
(2) update data
Db. users. update ({'name': 'xiangshu'}, {'$ set': {'sex': 'women'}, upsert = true, multi = false)
1. query Conditions
2. Updated Fields
Third: insert if no data exists
Fourth: whether to allow modification of multiple records
(3) Delete data
Db. users. remove ({'name': 'xumingxiang '})
(4) Delete all records
Db. users. remove ()
Delete collection
Db. users. drop () // If the deletion is successful, "true" is returned; otherwise, "false" is returned"
Delete current database
Db. dropDatabase ()
Comparison between NOSQL and SQL databases (PS: available on the Internet)
MongoDB syntax MySql syntax
Db. test. find ({'name': 'foobar'}) <=> select * from test where name = 'foobar'
Db. test. find () <=> select * from test
Db. test. find ({'id': 10}). count () <=> select count (*) from test where ID = 10
Db. test. find (). skip (10). limit (20) <=> select * from test limit 10, 20
Db. test. find ({'id': {$ in: [25, 35, 45] }}) <=> select * from test where ID in (25, 35, 45)
Db. test. find (). sort ({'id':-1}) <=> select * from test order by ID desc
Db. test. distinct ('name', {'id': {$ lt: 20 }}) <==> select distinct (name) from test where ID <20
Db. test. group ({key: {'name': true}, cond: {'name': 'foo'}, reduce: function (obj, prev) {prev. msum + = obj. marks ;}, initial: {msum: 0 }}) <=> select name, sum (marks) from test group by name
Db. test. find ('this. ID <20', {name: 1}) <=> select name from test where ID <20
Db. test. insert ({'name': 'foobar', 'age': 25}) <=> insert into test ('name', 'age') values ('foobar ', 25)
Db. test. remove ({}) <=> delete * from test
Db. test. remove ({'age': 20}) <=> delete test where age = 20
Db. test. remove ({'age': {$ lt: 20 }}) <=> elete test where age <20
Db. test. remove ({'age': {$ lte: 20 }}) <==> delete test where age <= 20
Db. test. remove ({'age': {$ gt: 20 }}) <=> delete test where age> 20
Db. test. remove ({'age': {$ gte: 20 }}) <=> delete test where age> = 20
Db. test. remove ({'age': {$ ne: 20 }}) <=> delete test where age! = 20
Db. test. update ({'name': 'foobar'}, {$ set: {'age': 36 }}) <==> update test set age = 36 where name = 'foobar'
Db. test. update ({'name': 'foobar'}, {$ inc: {'age': 3 }}) <==> update test set age = age + 3 where name = 'foobar'