Getting Started with MongoDB is simple (3)

Source: Internet
Author: User

# #简单的mongodb入门命令 # #

1.show dbs; View Current Database

2.use DatabaseName; Select Library

3.show tables/collections; See which tables are in the database

4.db.help (); View some of the operations commands for database

5.mongodb Library is created implicitly , we can use a non-existent library, and then create collection under the library, you can create the library;

For example: db.createcollection (' user ');
Db.user.insert ({id:10, Name: ' Wangwu ', age:20});
Db.user.find ();

6.collection allows implicit creation:
Db.collectonName.insert (document);

7. Delete collction;
Db.collectionName.drop ();

8. Delete database;
Db.dropdatabase ();



# #mongodb基本操作 # #


1. Add: Insert


Db.stu.insert ({stu_id: ' 001 ', Name: ' Xiaoqiang '});
Create multiple documents at once: JSON is an object, JS has the concept of arrays, we just need to put more than one object into an array can be;


For example:
Db.stu.insert ([{_id:3, SN: ' 003 ', Name: ' Liuqiang '},{sn: ' 006 ', Name: ' Zhaoming '}]);
We can write in any depth;



2. Delete: Remove


Syntax : db.collection.remove (query expression, options);
The option refers to whether (Just_one_line:true/false) deletes only one row, and the default is false;

For example:
Db.stu.remove ({sn: ' 001 '}); Remove the student with SN 001 in St Table
Db.stu.remove ({sn: ' 001 ', true}); Delete Only one row

Attention:
1) The query expression is still a JSON object;
2) The rows of the query expression matching will be deleted;
3) If the query expression is not written, all the documents in the collection will be erased;



3. Change: Update


Syntax : db.collection.update (query expression, assignment expression, new option);
Who to change--query expression
change to what--assignment expression
action option--optional parameter

For example:
Db.stu.update ({name: ' Dalang '}, {name: ' Zhagnqiang '}),     //New document replaces the old document directly
Db.stu.update ({name: ' Liuwei '}, {$set: {name: ' Yaoming '}});       //change only one column



An assignment expression when modified:
$set Modify the value of a column
$unset Delete a column
$rename Rename a column
$inc Grow a column
$setOnInsert The fields that can be supplemented when upsert is true and an insert operation has occurred;

Exp:

Db.stu.insert ({name: ' Zhangqiang ', Age16,sex: ' m ', weight:60});

Db.stu.update ({$set: {Name:wangli}, $unset: {age:17}, $rename: {sex: ' Gender '}, $inc: {weight:2}});


 
optional parameter:
  {upsert:false  multi:false}
  upsert: No matching rows are inserted directly into the row (if a field is queried to modify, otherwise new information is added)
   Muiti: Modify multiple rows (even if the query expression hits more than one line, and if you want to change multiple rows, add this option)
  
Exp:

Db.stu.update ({name: ' Zhangqiang '}, $set: {name: ' Lisi '},{upsert:true});

Db.stu.update ({name: ' Zhangqiang '}, $set: {name: ' Lisi ', $setOnInsert: {high:180}},{upsert:true});       //plus the High property

Db.stu.update ({gender: ' m '}, $set: {genter: ' Men '},{multi:true});


4. Check: Find,findone

Db.stu.find (); Querying all the Documents

Db.stu.find ({age:18}); Querying the columns of the age property in all documents

Db.stu.find ({}, {age:20}); Query age=20 columns in all documents without querying the ID attribute

Db.stu.find ({name: ' Zhngsan '}, {age:1,_id:0}); Find out that name is Zhangsan's age.

Getting Started with MongoDB is simple (3)

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.