MongoDB Learning II

Source: Internet
Author: User

Basic operations

Add and delete to verify a database necessary operations, next we say how to do in the shell under the increase and deletion check.

We also open a "command indicator" window (note: cannot be closed before), enter MONGO open MongoDB shell, this shell is the client of MongoDB

F:\mongodb\data\bin>mongoMongoDB shell version: 3.4.4connecting to: test>

The default connected database is "test"

  1. Increased operation

    Let's add a collection called "Person" (which can be understood as a database table)

    > db.Person.insert({"name":"张三", "age":10})WriteResult({ "nInserted" : 1 })> db.Person.insert({"name":"李四", "age":20})WriteResult({ "nInserted" : 1 })>
  2. Query operations

    Conditional query:

    > db.Person.find({"name":"张三"}){ "_id" : ObjectId("5617275737a5aa2cafdb4b84"), "name" : "张三", "age" : 10 }> 

    Query all:

    > db.Person.find(){ "_id" : ObjectId("5617277b37a5aa2cafdb4b85"), "name" : "李四", "age" : 20 }{ "_id" : ObjectId("56172d069d1af21ad1f769c7"), "name" : "张三", "age" : 40 }>

    Note : Did you find out? The collection of a "**_id**" field, this field is the default to join the GUID, the purpose is to ensure the uniqueness of the data, in the relational database, we will add a primary key to the table, in MongoDB can save themselves and the primary key.

  3. Update action

    The update command has two parameters, the first one is "condition found" and the second is "updated value"

    > DB.Person.update ({"Name": "Zhang San"}, { "name":  "Zhang San", " age ": 40}) writeresult ({  "nmatched": 1,  "nUpserted": 0,  "nmodified": 1}) > Db.person. find ({ "name":  "Zhang San"}) { "_id": objectid ( "56172d069d1af21ad1f769c7"), Span class= "hljs-string" > "name":  "Zhang San",  "age": 40}>             
  4. Delete operation

    > db.Person.remove({"name":"张三"})WriteResult({ "nRemoved" : 1 })> db.Person.find(){ "_id" : ObjectId("5617277b37a5aa2cafdb4b85"), "name" : "李四", "age" : 20 }

MongoDB Learning II

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.