MongoDB Configuration and basic operations

Source: Internet
Author: User
Tags mongodb port number

Mongodb
3.0 new Features
Wiredtiger
MMAPv1
Pluggable Engine API
Web-based visual management tools

View version number
Mongod--version
Start the database

Mongod--dbpath $dbpath--logpath $logpath--logappend--fork--port= 27000

$dbpath Data Catalog
$logpath is the path to the log file is not a directory path
Logappend Log Append method
--fork Background Run

Under the win
Add to System service

Mongod--dbpath $dbpath--logpath $logpath--logappend--install"MongoDb"

You can start with the service again
NET Start/stop MongoDB

Mongod--dbpath./data--logpath./log/mongo.log--logappend--fork

Operation

Start

Mongod--dbpath/usr/local/mongodb/data--logpath/usr/local/mongodb/log/mon.log--logappend--fork--port=27000 

Default Port 27017
Connection
MONGO remote host IP or DNS:MONGODB port number/database name-u user-p password
MONGO 127.0.0.1:27000
MONGO--port=27017
Querying all databases
Show databases
Shut down
On a client that is already connected
Use admin
Db.shutdownserver ()

Start with a configuration file

Establish mongodb.conf

#设置数据目录dbpath=/usr/local/mongodb/data# Setting the log file location LogPath=/usr/local/mongodb/log/mon.log# Open log output operation Logappend=true#不使用用户管理验证noauth=truePort=27017

Specify a configuration file to start
Mongod-f/usr/local/mongodb/mongodb.conf

-----------------------------------

Basic operations

1. Using the HK Database
> Use HK
Switched to DB HK
> Show Databases
Admin 0.000GB
Local 0.000GB
Use does not create a database when there is no actual data operation

2. Create a Collection

db.createcollection ("Stu")

> db.createcollection ("stu""OK"1  }> show databasesadmin  0. 000GBhk     0. 000GBlocal   0. 000GB

The HK database is then actually created

View all the data in the collection
Db.stu.find ()

3. Inserting data
Db.stu.insert ({"Name": ' HK ', ' age ': ten, ' Class ': 20});

> Db.stu.Find()> Db.stu.insert ({"name":'HK',' Age':Ten,'class': -}); Writeresult ({"ninserted":1 })> Db.stu.Find(){ "_id": ObjectId ("59295c842e2070afc7e34f3e"),"name":"HK"," Age":Ten,"class": -}

View Collections
> Show Collections
Stu

4. View HK Table Data
Db. Collection name. Find ({several conditions})
Inserting data from different structures

var data={   "name": "Zhang San",   "   Sex": "Male", "hobby": "Football",   " 中文版:;d b.stu.insert (data); >"_id": ObjectId ("59295c842e2070afc7e34f3e"), "name": "HK", "age": Ten, "Class":"_id": OBJ Ectid ("59295e3e2e2070afc7e34f3f"), "name": "Zhang San", "Sex": "Male", "hobby": "Soccer", "中文版": 89}

MongoDB each record generates _ID
Composition = timestamp + machine code +pid+ counter

5. View individual Document Information

> Db.stu.findOne () {    "_id": ObjectId ("59295c842e2070afc7e34f3e"),    "name": "HK",     "Age": Ten,    "class":

6. Delete data

Db.stu.remove ({"_id": ObjectId ("59295c842e2070afc7e34f3e")}); > Db.stu.remove ({"_id": ObjectId ("59295c842e2070afc7e34f3e""nremoved": 1 })>  "_id": ObjectId ("59295e3e2e2070afc7e34f3f"), "name": "Zhang San", "Sex": "Male", "hobby": "Soccer", "中文版": 89}

7. Updating data

var data={   "name": "Zhang San",   "sex": "female",   "hobby": "Dancing",   " 中文版 ":_id": ObjectId ("59295e3e2e2070afc7e34f3f")},data);

Operation Example

> var data={...    "name":"Zhang San",...    "Sex":"female",...    "Hobby":"Dancing",...    "中文版": About... };> Db.stu.update ({"_id": ObjectId ("59295e3e2e2070afc7e34f3f")},data); Writeresult ({"nmatched":1,"nupserted":0,"nmodified":1 })> Db.stu.Find(){ "_id": ObjectId ("59295e3e2e2070afc7e34f3f"),"name":"Zhang San","Sex":"female","Hobby":"Dancing","中文版": Aboutdocument update with completely different structure> var data={...      "a":'Ajax'... }> Db.stu.update ({"_id": ObjectId ("59295e3e2e2070afc7e34f3f")},data); Writeresult ({"nmatched":1,"nupserted":0,"nmodified":1 })> Db.stu.Find(){ "_id": ObjectId ("59295e3e2e2070afc7e34f3f"),"a":"Ajax"}
View Code

8. Deleting a collection

Db.stu.drop ()
> Db.stu.drop ()
True
> Show Collections

9. Delete the database (delete the current database, switch and re-operate)

Db.dropdatabase ()

MongoDB Configuration 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.