MONGODB Basic statement using

Source: Internet
Author: User

MongoDB Learning:
# #mongodb基础
# #数据库常用命令
# #用户相关
# #修改, add, delete collection data
# #条件操作符
# #创建表


-----------------------------------------------------------------------------
# #mongodb基础
MongoDB consists of databases (the equivalent of a MySQL database),
Databases consists of collections (collection equivalent to MySQL table)
Collections is made up of documents (document equivalent to MySQL line),
Documents are made up of fields (fields correspond to MySQL columns)


Bson stores data in MongoDB using a class JSON. Bson data can be understood to add some data types that are not in JSON, based on JSON.


MongoDB is writing data asynchronously


MongoDB does not have a command to create a database, but there are similar commands.
For example, if you want to create a "myTest" database, run the use myTest command first, then do something (such as: db.createcollection (' user ')) so that you can create a database called "MyTest".


-----------------------------------------------------------------------------
# #数据库常用命令


Enter help to see the basic Operations Command:


Help View command Prompt
Help
Db.help ();
Db.yourColl.help ();
Db.youColl.find (). Help ();
Rs.help ();


Switch/CREATE Database
Use Yourdb; The current database is created automatically when a collection (table) is created


Querying all databases
Show DBS;


View the database currently in use
db
Db.getname ();
The DB and GetName methods are the same effect and can query the currently used database


Show Current DB status
Db.stats ();


Create a Clustered collection (table)
Db.createcollection ("Collname", {size:20, Capped:5, max:100});
Db.createcollection ("MYTB");


Gets the Clustered collection (table) of the specified name
Db.getcollection ("account");


Get all the clustered collections of the current DB
Db.getcollectionnames ();


Displays the status of all clustered indexes in the current DB
Db.printcollectionstats ();


Current DB version
Db.version ();


View the link machine address of the current DB
Db.getmongo ();


Delete the currently used database
Db.dropdatabase ();


Repairing the current database
Db.repairdatabase ();


Cloning a database from a specified host
Db.clonedatabase ("127.0.0.1"); Clones data from a database on a specified machine to the current database


Copies the specified database data to a database from the specified machine
Db.copydatabase ("MyDB", "temp", "127.0.0.1"), copy the native mydb data to the TEMP database


Add a piece of data to the table:
Db.mytb.Save ({id:1,name: "tester"});


-----------------------------------------------------------------------------
# #用户相关
Add a user
Db.adduser ("name");
Db.adduser ("UserName", "pwd123", true); Add user, set password, read-only


Database authentication, security mode
Db.auth ("UserName", "123123");


Show all current users
Show Users;


Delete User
Db.removeuser ("UserName");


-----------------------------------------------------------------------------
# #修改, add, delete collection data
Add to
Db.users.save ({name: ' Zhangsan ', age:25, sex:true});
Data column of the added data, not fixed, according to the data added

Modify
Db.users.update ({age:25}, {$set: {name: ' ChangeName '}}, False, True);
Equivalent to: Update users set name = ' ChangeName ' where age = 25;

Db.users.update ({name: ' Lisi '}, {$inc: {age:50}}, False, True);
Equivalent to: Update users set age = Age + where name = ' Lisi ';

Db.users.update ({name: ' Lisi '}, {$inc: {age:50}, $set: {name: ' HoHo '}}, False, True);
Equivalent to: Update users set age = Age +, name = ' HoHo ' WHERE name = ' Lisi ';

Delete
Db.users.remove ({age:132});

Query modification Delete
Db.users.findAndModify ({
Query: {age: {$gte: 25}},
Sort: {Age:-1},
Update: {$set: {name: ' A2 '}, $inc: {age:2}},
Remove:true
});

Db.runcommand ({findandmodify: "Users",
Query: {age: {$gte: 25}},
Sort: {Age:-1},
Update: {$set: {name: ' A2 '}, $inc: {age:2}},
Remove:true
});


-----------------------------------------------------------------------------
# #条件操作符
(>) Greater than-$GT
(<) less than-$lt
(>=) greater than or equal to-$gte
(<=) less than or equal to-$lte


-----------------------------------------------------------------------------
#####################################
# #创建表
Use MyDB;
Db.adduser ("Snow", "Snow");
Db.auth ("Snow", "Snow");
Db.createcollection ("test");
Show collections;
Db.test.save ({id:1,name: "tester"});
Db.test.find ();
#####################################
Use MyDB;
Db.mydb.insert (document);
Document= ({"user_id": "Abcdbwn", "Password": "Abcdbwn", "Date_of_join": "15/10/2010", "Education": "B.C.A.", " Profession ":" DEVELOPER "," Interest ":" Music "," Community_Name ": [" Modern Music "," Classicalmusic "," WESTERN Music "]," community_moder_id ": [" Mr BBB "," Mr Jjj "," mrmmm "]," community_members ": [500,200,1500]," friends_id ": [" MMM123 "," NNN123 "," OOO123 "]," ban_friends_id ": [" BAN123 "," BAN456 "," BAN789 "]});
#####################################

MONGODB Basic statement using

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.