Basics of MongoDB Entry DML

Source: Internet
Author: User
Tags mongodb collection

Objective:

This article mainly records the DML operations of the main MongoDB Collection.

The Collection name in the second article is Yourcoll, and each operation initially contains two data

{    "_id": ObjectId ("5438df36309dca34635d4460"),    "username":"name1",    "Mail":"Name1@abc. com"},{    "_id": ObjectId ("5438df40309dca34635d4461"),    "username":"name2",    "Mail":"[email protected]",    " Age": -}
A, C
    • Db.collection.insert ()
      Db.yourcoll. Insert ({    username: "Mayj",    Mail: "Test@abc. com"})
    • Db.collection.save ()
      Db.yourcoll. Save ({    username: "Mayj",    Mail: "Test@abc. com"})

      Note:save operation if the document contains a primary key (_ID), to determine whether the primary key exists, if present, update the original record, if not present, insert a new record. The difference between insert and save is that if a document contains a primary key (_id) and exists, it does not handle processing and returns an error.

Second, R
  • Db.collection.find ()
    • Querying All records
      Db.yourColl.find ();

      Equivalent to MySQL

      Select *  from Yourcoll;
    • Querying distinct duplicate data for a column
      Db.yourcoll. distinct

      Equivalent to MySQL

      Select  from
    • Querying the AGE=28 data
      Db.yourColl.find ({    ,})

      Equivalent to MySQL

      Select *  from where = ;
    • Query Age >20 data
       db.yourcoll.find ({age: {$gt:  20   

      Equivalent to MySQL

        *  from  yourcoll where  Age > 20 ; 
    • Query Age >=20 data
       db.yourcoll.find ({age: {$gte:  20   

      Equivalent to MySQL

        *  from  yourcoll where  Age >=  20 ;
    • Query Age >=20 and age < 30 data
       db.yourcoll.find ({age: {$gte:  20 , $lt: }})  

      Equivalent to MySQL

       select  *  from  yourcoll where  Age >=  20  and  age <  30 ;   
    • query for data in mail that contains ABC
       db.yourcoll.find ({mail:  / Abc/i   

      Equivalent to MySQL

       select  *  from  yourcoll like " Span style= "color: #808080;" >%  abc%  "; 
    • query username for data that begins with name
       db.yourcoll.find ({mail:  /^  abc/ i})  

      Equivalent to MySQL

       select  *  from  yourcoll like " Span style= "color: #808080;" >%^  abc%  "; 
    • query username =name1,mail [email protected] Data
       db.yourcoll.find ({ Username: "Name1", Mail: "name1   @abc  .com "})  

      Equivalent to MySQL

       select   from  yourcoll where  username Span style= "color: #808080;" >=  "name1" and  mail =  "Name1 @abc . com "; 
    • Querying data for the specified column username and mail
      Db.youColl.find ({    },{    username:true,    1})

      Equivalent to MySQL

      Select  from Yourcoll;

      Where true can be replaced by 1.

    • By username ascending, mail descending order
      Db.yourColl.find ({}). Sort ({username:1, mail:-1})

      Equivalent to MySQL

      Select *  from Order  by ASC DESC;
    • Query 1-2 Data
       db.yourColl.find ({}). Limit (2 ). Skip (1 ) 

      Equivalent to MySQL

       select  *  from  yourcoll limit Span style= "color: #800000; Font-weight:bold; " >1 , 1    
    • Query username = name1 or username =name2 data

       db.yourcoll.find ({$  
             
              or : 
              [ 
              {username: "Name1"}, {username: " Name2 "}  
                
              })  
             

      Equivalent to MySQL

       select  *  from  yourcoll = " Name1 "or  username = " name2 "; 
    • Query the number of records that match the criteria
      Db.collect1.find ({}). Count ()

      Equivalent to MySQL

      Select Count (* from Yourcoll
  • Db.collection.findOne ()

  • Db.collection.findAndModify ()
Three, U
  • Db.collection.update ()
    • Update username = Name2 User has an age of $
       db.yourcoll.  set   30 False, 
      True)

      equals MySQL

       update  yourcoll =  30  where  username =  "name2"; 
    • Update username = Name2 User's age is age +
       db.yourcoll. Update   set   30 False, 
      True)

      equals MySQL

       update  yourcoll =  Age Span style= "color: #808080;" >+  30  where  Username =  "name2"; 
    • Update username = Name2 User's username = Name3 Age is age + 30
      Db.yourcoll. Update (    {        username: "Name2"    },    {        $set: {            $        },        $inc: {            username: "Name3"        }    }, False, True)

      Equivalent to MySQL

      Update Set = +  - = where = "Name2";
IV, D
    • Db.collection.remove ()
      • Delete username = Name3 Records
        Db.yourColl.remove ({  username: "Name3"})

        Equivalent to MySQL

        Delete  from where = "Name3";
Wu, Rud
  • Db.collection.findAndModify () & Db.collection.runCommond ()
  •  db.users.findandmodify ({query: {age: {$gte:  25   - 1   update : {$set : {name:  " a2   ' }, $inc: {age: 2  }} , remove:true}); 
    Db.runcommand ({     findandmodify: "Users",          1},      Update: {$set'a2'2}},     
    Parametersdetailed default ValuesQueryQuery filter condition {} Sortif multiple documents conform to the query filter criteria, the first-ranked object is selected in the arrangement specified by the parameter, and the object is manipulated {} RemoveIf True, the selected object will be deleted before returning n /AUpdatea Modifier object n /aNewIf True, the modified object is returned instead of the original object. In the delete operation, the parameter is ignored. false

External resource Links:

MongoDB CRUD Introduction

Basics of MongoDB Entry DML

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.