MongoDB Basic Operations (i)

Source: Internet
Author: User
Tags findone

1.1. MongoDBBasic Operations1.1.1. Create a database

Syntax: Use [DatabaseName]

> Use admin

Switched to DB admin

> Use RGF; # CREATE DATABASE RGF

Switched to DB RGF

> Db.person.insert ({name: ' Xuanxuan '}) # Create a collection person in RGF and insert a row of data

Writeresult ({"ninserted": 1})

1.1.2. View all Databases

Syntax: Show DBS

> Show DBS;

Admin (empty)

Local 0.078GB

RGF 0.078GB

Test 0.078GB

1.1.3. add a collection to the specified database and add records

Syntax: db.[ Documentname].insert ({...})

> Db.person.insert ({age: ' 10 '});

Writeresult ({"ninserted": 1})

>db.person.insert ({age: '},{sex: ' Male '});

Writeresult ({"ninserted": 1})

1.1.4. View all documents in the database

Syntax: showcollections;

> Show Collections;

Person

System.indexes

1.1.5. querying data for a specified document

Grammar:

Check all :

Db. [Documnetname].find ();

Query the first piece of data:

Db. [Documentname].findone ()

> Db.system.indexes.find ()

{"V": 1, "key": {"_id": 1}, "name": "_id_", "ns": "Rgf.person"}

> Db.person.find ()

{"_id": ObjectId ("549b31b7ff3a181677422f47"), "name": "Xuanxuan"}

{"_id": ObjectId ("549b32ebff3a181677422f48"), "Age": "10"}

{"_id": ObjectId ("549b3352ff3a181677422f49"), "Age": "10"}

> Db.person.insert ({age: '},{sex: ' Male '});

Writeresult ({"ninserted": 1})

> Db.person.findone ()

2014-12-25t05:58:14.270+0800 typeerror:property ' FindOne ' of object Rgf.person is not a function

> Db.person.findOne ()

{"_id": ObjectId ("549b31b7ff3a181677422f47"), "name": "Xuanxuan"}

Note that the O in the function FindOne must be capitalized.

1.1.6. Update document Data

Grammar:

db. [Documentname].update ({ query condition },{ update content })

Cases:

VarP = Db.person.findOne ()

Db.person.update (p,{name: "Gaogao"})

>db.person.update ({name: ' Xuanxuan '},{$set: {name: ' Zhaoxing '}})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

> Db.person.findOne ()

{"_id": ObjectId ("549b31b7ff3a181677422f47"), "name": "Zhaoxing"}

>

> var p = db.person.findOne ()

> P

{"_id": ObjectId ("549b31b7ff3a181677422f47"), "name": "Zhaoxing"}

>db.person.update (p,{name: "Gaogao"})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

> P

{"_id": ObjectId ("549b31b7ff3a181677422f47"), "name": "Zhaoxing"}

> var p = db.person.findOne ()

Note that you need to assign variables again at this time

> P

{"_id": ObjectId ("549b31b7ff3a181677422f47"), "name": "Gaogao"}

>

{"_id": ObjectId ("549b31b7ff3a181677422f47"), "name": "Gaogao2", "Age": 2}

MONGO changes the first data by default.

>db.person.update ({age: "},{$set: {age:5}})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

> Db.person.find ()

{"_id": ObjectId ("549b32ebff3a181677422f48"), "Age": 5}

{"_id": ObjectId ("549b3352ff3a181677422f49"), "Age": "10"}

{"_id": ObjectId ("549b3508ff3a181677422f4a"), "Age": "20"}

{"_id": ObjectId ("549b31b7ff3a181677422f47"), "name": "Gaogao2", "Age": 2}

>

1.1.7. Delete data in a document

Grammar:

Db. [Documentname].remove ({...})

> Db.person.find ()

{"_id": ObjectId ("549b32ebff3a181677422f48"), "Age": 5}

{"_id": ObjectId ("549b3352ff3a181677422f49"), "Age": "10"}

{"_id": ObjectId ("549b3508ff3a181677422f4a"), "Age": "20"}

{"_id": ObjectId ("549b31b7ff3a181677422f47"), "name": "Gaogao2", "Age": 2}

> Db.person.remove ({Age: "10"})

Writeresult ({"nremoved": 1})

> Db.person.find ()

{"_id": ObjectId ("549b32ebff3a181677422f48"), "Age": 5}

{"_id": ObjectId ("549b3508ff3a181677422f4a"), "Age": "20"}

{"_id": ObjectId ("549b31b7ff3a181677422f47"), "name": "Gaogao2", "Age": 2}

>


MongoDB Basic Operations (i)

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.