MongoDB installation and basic operations

Source: Internet
Author: User
Tags findone mongodb client

a . Preparation before use ( installation under Windows)

1. Download

At present MongoDB 's official website does not know to ask what cannot carry on the download, but can download in the mongodb Chinese Forum,

The address is as follows:http://www.mongoing.com/mongodb-downloads

2. Installation

I downloaded the MSI version of the software, the installation process to choose a custom way, you can change the location of the installation, my installation path is as follows:

The following files will appear when the installation is complete

3. start MongoDB

Open cmdand go to the installed MongoDB bin directory to execute the following command:

The following --dbpath parameter is the folder where the data is stored

4. MongoDB can also be installed as a Windows service , you can refer to the garden in the practice of a great God:http://www.cnblogs.com/ lzrabbit/p/3682510.html

A little different is that I read the great God article: Because I was the first to install, so directly write the following command to install it

D:\mongodb\bin>mongod.exe--dbpath D:\MongoDBData--logpath=d:\mongodblog\mongod

B.log--logappend--install

You can then view our installed MongoDB service in the services in the Administration tool

5. start the configuration file for the database service

Mongodb is configured and accessed through configuration files under Windows

Mongodb starting command mongod parameter description

6. start the MongoDB client via cmd

Start the client can operate the database, the following are some basic operations.

Two . Using Mongodb

① Creating a Database

use[ database name ]

If you do nothing at this time, the newly created database will be deleted if you exit directly.

② add a collection to the database and add a record to the collection

db.[ file name ].insert ({...})

> DB. Student.insert ({name: "STU1", Age: "15"})

③ View all databases

Show DBS

> Show Dbsadmin        (empty) local        0.078GBmongoDBTest  0.078GB

④ viewing all documents in a database

Show Collections

> Show collectionsstudentsystem.indexes

  

⑤ viewing data for a specified document

Query all data : db.[ file name ].find ()

Query the First Data : db.[ file name ].findone ()

> DB. Student.find () {"_id": ObjectId ("56D4FFA1DAC7A0AD749F16BF"), "name": "Stu1", "Age": "{"} {"_id": ObjectId ("56d503c7d Ac7a0ad749f16c0 ")," name ":" Stu2 "," Age ":"}> db ". Student.findone () {        "_id": ObjectId ("56D4FFA1DAC7A0AD749F16BF"),        "name": "STU1",        "age": "}>"

⑥ updating of document data ( two ways )  

1) Full update, is to overwrite the original data

Method:

Db. [ filename ].update ({ query condition },{ update content })

Cases:

1. check the records in the file first, there are two

2. create a variable and assign the first record to it

3. Update the data

4. View variables and updated records after updating

5. The result is covered, so the data is missing .

2) using the set modifier

Db. [ filename ].update ({ query condition },{$set: { update content }})

For example, I modified the name=stu1 age=15 of the above to change his age to five years old .

> DB. Student.update ({name: "STU1"},{$set: {Age: "}}") Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) > db. Student.findone () {        "_id": ObjectId ("56D4FFA1DAC7A0AD749F16BF"),        "name": "STU1",        "age": "}>"

and SQL statements.

Update table name set age=20 where name= "Stu1" almost

⑦ deleting data in a document

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

> DB. Student.remove ({job: "Student"}) Writeresult ({"nremoved": 1}) > var s = db. Student.find () > s{"_id": ObjectId ("56d503c7dac7a0ad749f16c0"), "name": "Stu2", "Age": "}>"

⑧ Deleting a database file

Db. [ filename ].drop ()

⑨ Deleting a database

Db.dropdatabase ()

Add:

ways to view MongoDB

Db.help () Use this function to view

Data collection Naming conventions note points:

You can give the collection a name of Db-test, but with db.[ file name ] will go wrong

You can use DB. Getcollectio (" file name ") to

Example:

> Db.db-test.insert ({name: "123"}) 2016-03-01t11:41:30.032+0800 Referenceerror:test is not defined> Db.getcollection ("Db-test"). Insert ({name: "123"}) Writeresult ({"ninserted": 1})

  

MongoDB 's shell comes with a JavaScript engine, so you can write javascript functions in the shell

Example :

> Function Insert (object) {db.getcollection ("Student"). Insert (object);} > Insert ({name: "xxxxx"}) > Db.getcollection ("Student"). Find () {"_id": ObjectId ("56d503c7dac7a0ad749f16c0"), " Name ":" Stu2 "," Age ":" {"} {" _id ": ObjectId (" 56d51208dac7a0ad749f16c2 ")," name ":" XXXXX "}>

Iii. Summary

Just learned the basic operation of Mongodb, some of which have to learn

1. when we see that we are creating a collection,Mongodb automatically generates a system. collection of indexs

2. each record will automatically generate a key/value,_id and objectId

3. every time I insert data, I use something like JSON , which is called BSON, whichis understood to be an extension of JSON .

MongoDB installation and basic operations

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.