Use MongoDB (1)

Source: Internet
Author: User
Tags findone

 

MongoDB is a product between relational databases and non-relational databases. It has the most abundant functions and features like relational databases. The supported data structure is very loose and is similar to the JSON bjson format. Therefore, it can store complicated data types. The biggest feature of Mongo is that it supports a very powerful query language. Its syntax is somewhat similar to an Object-Oriented Query Language. It can almost implement most of the functions similar to single-table queries in relational databases, it also supports data indexing.

It features high performance, ease of deployment, and ease of use, making it easy to store data.

 

1. MongoDB acquisition and installation

 

(1) Get the address http://www.mongodb.org/downloads according to their needs to select the corresponding version, Linux can use wget command.

(2) unzip the mongodb-win32-i386-1.8.1

(3) create a data storage folder. The default data directory/data/DB of MongoDB

C:/> mkdir/Data

C:/> mkdir/data/DB

(4) Run MongoDB

Mongod.exe-database server, equivalent to MySQL mysqld command, start the server

Cmd.exe-database client, which is equivalent to MySQL Command. Open the management console.

 

Start the service

Mongod.exe -- dbpath F:/database/MongoDB/DB/

-- Dbpath: data file storage path

-- Port Data Service port

C:/> Cd/my_1__dir/bin

C:/my_cmd_dir/bin> mongod // start the mongod server. Default database path/data/DB, port 27071

Start the client

Cmd.exe cclove

Name of the database connected to cclove

C:/> Cd/my_1__dir/bin

C:/my_1__dir/bin> Mongo

 

2. Familiar with MongoDB data operation statements, such as SQL

 

Database Operation syntax

Mongo -- path

DB. adduser (username, password) Add User

DB. Auth (usrename, password) sets database connection Verification

DB. clonedatabase (fromhost) clone a database from the target server

DB. commandhelp (name) returns the help for the command

DB. copydatabase (fromdb, todb, fromhost) copy the database fromdb --- source database name, todb --- target database name, fromhost --- source database server address

DB. createcollection (name, {size: 3333, capped: 333, Max: 88888}) to create a dataset, equivalent to a table

DB. currentop () cancels the current operation of the current database

DB. dropdatabase () Delete the current database

DB. eval (func, argS) run code server-side

DB. getcollection (cname) obtains a data set. The same usage is DB ['cname'] or db. cname.

DB. getcollenctionnames () Get the name list of all data sets

DB. getlasterror () returns the message indicating the last error.

DB. getlasterrorobj () returns the last error object

DB. getmongo () gets the connection object of the current server get the server connection object

DB. getmondo (). setslaveok () allow this connection to read from then nonmaster membr of a replica pair

DB. getname () returns the name of the database to be operated on.

DB. getpreverror () returns the previous error object

DB. getprofilinglevel ()? Level

DB. getreplicationinfo ()? What information

DB. getsisterdb (name) Get the dB at the same server as this onew

DB. Killop () Stop (kill) the current operation in the current database

DB. printcollectionstats () returns the dataset status of the current database

DB. printreplicationinfo ()

DB. printslavereplicationinfo ()

DB. printshardingstatus () returns whether the current database is a shared database

DB. removeuser (username) delete a user

DB. repairdatabase () repairs the current database

DB. reseterror ()

DB. runcommand (cmdobj) run a database command. If cmdobj is a string, turns it into {cmdobj: 1}

DB. setprofilinglevel (level) 0 = OFF, 1 = slow, 2 = all

DB. shutdownserver () Close the current service program

DB. Version () returns the version information of the current program.

 

Dataset (table) operation syntax

DB. Linlin. Find ({ID: 10}) returns the Linlin dataset id = 10

DB. Linlin. Find ({ID: 10}). Count () returns the total number of data records with Linlin dataset id = 10.

DB. Linlin. Find ({ID: 10}). Limit (2) returns the dataset whose Linlin dataset id = 10 starts from the second one.

DB. Linlin. Find ({ID: 10}). Skip (8) returns the data set of the Linlin dataset id = 10 from 0 to the eighth.

DB. Linlin. Find ({ID: 10}). Limit (2). Skip (8) returns the data of the Linlin dataset id = 1 = from the second to the eighth.

DB. Linlin. Find ({ID: 10}). Sort () returns the sorted dataset with the Linlin dataset id = 10.

DB. Linlin. findone ([query]) returns a data record that meets the condition.

DB. Linlin. getdb () returns the name of the database to which the dataset belongs.

DB. Linlin. getindexes () returns index information for some datasets.

DB. Linlin. Group ({key:..., initial:..., reduce:... [, Cond:...]})

DB. Linlin. mapreduce (mayfunction, performancefunction, <optional Params>)

DB. Linlin. Remove (query) deletes a piece of data in the dataset.

DB. Linlin. renamecollection (newname) rename some dataset names

DB. Linlin. Save (OBJ) inserts a data entry into the dataset.

DB. Linlin. Stats () returns the status of this dataset

DB. Linlin. storagesize () returns the storage size of this dataset.

DB. Linlin. totalindexsize () returns the size of the index file for this dataset.

DB. Linlin. totalsize () returns the total size of some datasets.

DB. Linlin. Update (query, object [, upsert_bool]) updates a piece of data in this dataset.

DB. Linlin. Validate () verifies this dataset

DB. Linlin. getshardversion () returns the shared version of the dataset.

 

DB. Linlin. Find ({'name': 'foobar'}) Select * From Linlin where name = 'foobar'

DB. Linlin. Find () Select * From Linlin

DB. Linlin. Find ({'id': 10}). Count () Select count (*) from Linlin where id = 10

DB. Linlin. Find (). Skip (10). Limit (20) reads 20 data records starting from the tenth query result select * From Linlin limit ---------- MySQL

DB. Linlin. Find ({'id': {$ in: [, 45]}) Select * From Linlin where ID in (, 45)

DB. Linlin. Find (). Sort ({'id':-1}) Select * From Linlin order by ID DESC

DB. Linlin. Distinct ('name', {'id': {$ LT: 20}) Select distinct (name) from Linlin where ID <20

 

DB. linlin. group ({key: {'name': true}, Cond: {'name': 'foo'}, reduce: function (OBJ, Prev) {Prev. msum + = obj. marks;}, initial: {msum: 0 }})

Select name, sum (marks) from Linlin group by name

DB. Linlin. Find ('this. ID <20', {Name: 1}) Select name from Linlin where ID <20

 

DB. linlin. insert ({'name': 'foobar', 'age': 25}) insert into Linlin ('name', 'age') values ('foobar', 25)

DB. Linlin. insert ({'name': 'foobar', 'age': 25, 'email ': 'cclove2 @ 163.com '})

 

DB. Linlin. Remove ({}) delete * From Linlin

DB. Linlin. Remove ({'age': 20}) delete Linlin where age = 20

DB. Linlin. Remove ({'age': {$ LT: 20 }}) delete Linlin where age <20

DB. Linlin. Remove ({'age': {$ LTE: 20}) delete Linlin where age <= 20

DB. Linlin. Remove ({'age': {$ GT: 20}) delete Linlin where age> 20

DB. Linlin. Remove ({'age': {$ GTE: 20}) delete Linlin where age> = 20

DB. Linlin. Remove ({'age': {$ ne: 20}) delete Linlin where age! = 20

 

DB. linlin. update ({'name': 'foobar'}, {$ set: {'age': 36}) Update Linlin set age = 36 where name = 'foobar'

DB. linlin. update ({'name': 'foobar'}, {$ Inc: {'age': 3 }}) update Linlin set age = age + 3 where name = 'foobar'

 

Table of Operation statements officially provided:

 

Upstream: SQL statement

Downstream: Mongo operation statement

Create Table users (a number, B number)

DB. createcollection ("mycoll ")

 

Insert into users values (1, 1)

DB. Users. insert ({A: 1, B: 1 })

 

Select a, B from users

DB. Users. Find ({}, {A: 1, B: 1 })

 

Select * from users

DB. Users. Find ()

 

Select * from users where age = 33

DB. Users. Find ({age: 33 })

 

Select a, B from users where age = 33

DB. Users. Find ({age: 33}, {A: 1, B: 1 })

 

Select * from users where age = 33 order by name

DB. Users. Find ({age: 33}). Sort ({Name: 1 })

 

Select * from users where age> 33

DB. Users. Find ({'age': {$ GT: 33 }})})

 

Select * from users where age <33

DB. Users. Find ({'age': {$ LT: 33 }})})

 

Select * from users where name like "% Joe %"

DB. Users. Find ({Name:/Joe /})

 

Select * from users where name like "Joe %"

DB. Users. Find ({Name:/^ Joe /})

 

Select * from users where age> 33 and age <= 40

DB. Users. Find ({'age': {$ GT: 33, $ LTE: 40 }})})

 

Select * from users order by name DESC

DB. Users. Find (). Sort ({Name:-1 })

 

Select * from users where a = 1 and B = 'Q'

DB. Users. Find ({A: 1, B: 'q '})

 

Select * from users limit 10 skip 20

DB. Users. Find (). Limit (10). Skip (20)

 

Select * from users where a = 1 or B = 2

DB. Users. Find ({$ or: [{A: 1 },{ B: 2}]})

 

Select * from users limit 1

DB. Users. findone ()

 

Select distinct last_name from users

DB. Users. Distinct ('Last _ name ')

 

Select count (* Y) from users

DB. Users. Count ()

 

Select count (* Y) from users where age> 30

DB. Users. Find ({age: {'$ GT': 30}). Count ()

 

Select count (AGE) from users

DB. Users. Find ({age: {'$ exists': true}). Count ()

 

Create index myindexname on users (name)

DB. Users. ensureindex ({Name: 1 })

 

Create index myindexname on users (name, ts desc)

DB. Users. ensureindex ({Name: 1, TS:-1 })

 

Explain select * from users where z = 3

DB. Users. Find ({z: 3}). Explain ()

 

Update users set a = 1 Where B = 'Q'

DB. Users. Update ({B: 'q'}, {$ set: {A: 1 }}, false, true)

 

Update users set a = a + 2 Where B = 'Q'

DB. Users. Update ({B: 'q'}, {$ Inc: {A: 2 }}, false, true)

 

Delete from users where z = "ABC"

DB. Users. Remove ({z: 'abc '});

 

 

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.