Start MongoDB journey
Luo Weifeng 2011-6-28
Environment: Ubuntu 11.04
Install MongoDB
$ Sudo apt-Get install MongoDB
Libpcrecpp0 libboost-system1.42.0 libboost-filesystem1.42.0 is automatically installed
Libboost-program-options1.42.0 libboost-thread1.42.0 xulrunner-2.0-mozjs
Dependent packages such as MongoDB-clients MongoDB-server MongoDB-dev MongoDB.
$ PS aux | grep primary d
Install the python driver
$ Sudo apt-Get install Python-setuptools
$ Sudo easy_install pymongo
Configure MongoDB
$ Sudo Vim/etc/MongoDB. conf
Dbpath = 'your datebase Path'
Logpath = 'Where to Log'
Logappend = true
Bind_id = 127.0.0.1
Port = 27017
Use Mongo to test the database
Mongo is the client, and mongod is the server. The following uses Mongo to test the service.
Root @ Ubuntu :~ # Mongo
MongoDB shell version: 1.6.3
Mon Jun 27 19:15:05 *** warning: Spider Monkey build without utf8 support. consider rebuilding with utf8 support
Connecting to: Test
>
> DB. serverstatus ()
There are several main types of output parameters in JSON format:
Uptime: server running time (seconds)
Localtime: local server time
Mem: Server Memory Information
Connections: current number of connections
Opcounters: Operation statistics
View All databases:
> Show DBS
Admin
Local
It can be seen that at the beginning, only the admin local test database was used, and test was not displayed here.
Switch Database
> Use Admin
Switched to DB Admin
> DB. Stats ()
Use Web testing database
Access 27017
Access 27017 + 1000 = 28017 again as required
You can see a complete management page, which is obviously not as beautiful as couchdb.
Complete Test to create a database
MongoDB does not have the command to create a database. You can use dbname to switch to a database that does not exist. when data is written, a database is created.
Root @ Ubuntu :~ # Mongo
> Use mytestdb
Create collection
The coolection database is created after you enter the database. Use
DB. createcollection ("mytestdb", {capped: True, size: 10000}) is measured in KB.
Or dB. runcommand ({createcollection: "mytestdb", capped: True, size: 100000 })
The capped parameter is used to create a database file of a fixed size. To ensure efficiency, Mongo occupies disk space during collection creation to prevent fragments.
> DB. createcollection ("mytestdb", {capped: True, size: 10000 })
> Show collections
Mytestdb
Document creation
> DB. mytestdb. insert ({Name: 'xiaowanzi', age: 8 })
Sample Query
Operator |
SQL |
Mongo |
* |
Select * From mytestdb |
DB. mytestdb. Find () |
Column |
Select name, age from mytestdb |
DB. mytestdb. Find ({}, {Name: 1, age: 1 }) |
Where * |
Select * From mytestdb where age = 24 |
DB. mytestdb. Find ({age: 24 }) |
Column where |
Select name, age from mytestdb where age = 24 |
DB. mytestdb. Find ({age: 24}, {Name: 1, age: 1 }) |
> & Gt |
Select * From mytestdb where age> 20 |
DB. mytestdb. Find ({'age': {& gt: 20 }}) |
Like |
Select * From mytestdb where name like 'wang %' |
DB. mytestdb. Find ({Name:/^ wangfan /}) |
|
Select * From mytestdb where name like '% Wang %' |
DB. mytestdb. Find ({Name:/wangfan /}) |
Order |
Select * From mytestdb order by name DESC |
DB. mytestdb. Find (). Sort ({Name:-1 }) |
> DB. mytestdb. Find ()
{"_ Id": objectid ("4e093ff90edf95f31cbc7c29"), "name": "xiaowanzi", "Age": 8}
Create an index
Use ensureindex to create an index
DB. mytestdb. ensureindex ({Name: 1 })
DB. runcommand ({dropindexes: 'foo', index :'*'})
Here, 1 is positive, and-1 is inverted.
Delete Index
DB. collection. dropindexes (); Delete All indexes
DB. mytestdb. dropindexes ({Name: 1 });
DB. runcommand ({dropindexes: 'wfcoll ', index: {Name: 1 }})
We create an index for the name field and a negative index for the age, as shown below:
> DB. mytestdb. ensureindex ({Name: 1 })
> DB. mytestdb. ensureindex ({age:-1 })
Test with Python
$ Python
>>> Import pymongo
>>> Conn = pymongo. Connection (host = "localhost", Port = 27017)
>>> DB = conn. mytestdb
>>> For user in db. wfcoll. Find ({}):
... Repr (User)