Windows uses MongoDB, and index creation

Source: Internet
Author: User
Tags create index prev

installation MongoDB

Https://www.mongodb.com/download-center#community

Click the MSI installer to install, you can do a custom installation, choose the installation location, I chose D Drive

in the D. Create a data\db directory under the packing directory to store the data; Create a data\dbconf\mongodb.log file to hold the MongoDB log

Double-click Run MongoDB Server

to run the MongoDB server as a Windows service

execute the following command to The MongoDB server runs as a Windows Service:

Mongod.exe--bind_ip 127.0.0.1--logpath "D:\data\dbConf\mongodb.log"--logappend--dbpath "D:\data\db"--port 27017-- ServiceName "myServiceName"--servicedisplayname "myServiceName"--install

The following table describes the parameters for MongoDB startup:

Parameters

Describe

--bind_ip

Bind service IP, if bind 127.0.0.1, can only native access, do not specify default local all IP

--logpath

Specify the MongoDB log file, note that the specified file is not a directory

--logappend

Write a log using an Append method

--dbpath

Specify the database path

--port

Specify the service port number, default port 27017

--servicename

Specify the service name

--servicedisplayname

Specifies the service name that is executed when there are multiple MongoDB Services.

--install

Specifies the installation as a Windows service.

mongdb installation of graphical tools (robomong )

Http://www.softpedia.com/get/Internet/Servers/Database-Utils/Robomongo.shtml#download

Click Next to install

Simple and practical use of mongdb

Double-click Mongo.exe

1. Create a database

Use database_name

If the database does not exist, create the database, or switch to the specified database

2. View the database

Show DBS

3. Insert the data document ( with the collection inserted directly, without the collection, automatically created )

Db. the name of the collection . Insert ({

Name: ' Zhang San ',

Age:27

}

)

Eg:

4. Update the document

Db. Collection Name . Update (

{' name ': ' Zhang San '} , {$set: {' name ': ' Lisi '}}

)

5. Enquiry

Db. the name of the collection . Find ()

6. Delete

To delete a collection named Zhang San

Db.col.remove ({' name ': ' Zhang San '})

Delete the records found in the first article

Db. the name of the collection . Remove (deletion_criteria,1)

Delete all data

Db. the name of the collection . Remove ({})

6. Conditional Query

Db. the name of the collection . Find ({likes:{$gte: +})

Similar

SELECT * from col where likes>=100

less than ---$lt

less than or equal to----$lte

Clear Range query is greater than

Db. the name of the collection . Find ({likes:{$lt: $gt:)})

7.limit Query

Db. Collection_name.find (). Limit (number)

Db. Collection name. Find ({},{"title": 1,_id:0}). Limit (2)

8. sort (1 Ascending -1 Descending)

Db. Collection name. Find (). Sort ({key:1})

comparison between MongoDB syntax and existing relational database SQL Syntax

MongoDB Grammar MySql syntax

Db.test.find ({' name ': ' Foobar '}) <==> select * from test where name= ' Foobar '

Db.test.find () <==> Select *from test

Db.test.find ({' ID ': ten}). Count () <==> Select COUNT (*) from Test where id=10

Db.test.find (). Skip () Limit <==> select * FROM Test limit 10,20

Db.test.find ({' ID ': {$in: [25,35,45]}} <==> select * from Test where ID in (25,35,45)

Db.test.find (). Sort ({' ID ': -1}) <==> select * FROM Test ORDER by Iddesc

Db.test.distinct (' name ', {' ID ': {$lt:}}) <==> select DISTINCT (name) from Testwhere id<20

Db.test.group ({key:{' name ': ' true},cond:{' name ': ' foo '},reduce:function (obj,prev) {prev.msum+=obj.marks;},initial:  {msum:0}}) <==> Select Name,sum (Marks) from Testgroup by name

Db.test.find (' this.id<20 ', {name:1}) <==> select name from Test whereid<20

Db.test.insert ({' name ': ' Foobar ', ' age ':] <==>insertinto Test (' name ', ' age ') VALUES (' Foobar ', 25)

Db.test.remove ({}) <==> Delete * from test

Db.test.remove ({' Age ': ') ' <==> delete test where age=20

Db.test.remove ({' age ': {$lt:}}) <==> elete test where age<20

Db.test.remove ({' age ': {$lte:}}) <==> delete test where age<=20

Db.test.remove ({' age ': {$gt:}}) <==> delete test where age>20

Db.test.remove ({' age ': {$gte:}}) <==> delete test where age>=20

Db.test.remove ({' age ': {$ne:}}) <==> delete test where age!=20

Db.test.update ({' name ': ' Foobar '},{$set: {' Age ': '} ') <==> update test set age=36 where Name= ' Foobar '

Db.test.update ({' name ': ' Foobar '},{$inc: {' Age ': 3}} ' <==> update test set age=age+3 where Name= ' Foobar '

fuzzy query:$regex

Db.test.find ({"name": {$regex: "AAA"}})

MongoDB Index related

View index:

Db.getcollection (' Id_mapper '). Getindexes ()

CREATE INDEX:1 to create an index in ascendingorder-1 for index

Db.getcollection (' Id_mapper '). Ensureindex ({"contract_id":1},{background:true})

Note: If you are creating an index on a document that already has data, you can have the background value trueto enable MongoDB to create indexes in the background, so that no other operations are blocked when created. However, creating an index in a blocking way can make the entire creation process more efficient, but MongoDB will not be able to receive other operations when it is created.

The default index name is:contract_id_1

To create a composite unique index:

Db.getcollection (' Id_mapper '). Ensureindex ({"apply_id": 1, "Insti_code": 1},{"Background": true},{"unique": true})

Delete index: Before deleting, be sure to watch what the index name is

Db.getcollection (' Id_mapper '). Dropindex ("Contract_id_1")

Db.getcollection (' Id_mapper '). Dropindexes () Delete all indexes

Two-field query:

Db.getcollection (' Id_mapper '). Find ({"contract_id": "767862ce-0ca9-4673-92e5-c505d7d3686c"},{"Insti_code": "1"})

There's a field that has an index on it.

After the index is created, the new document added by the program is automatically indexed and verified

Windows uses MongoDB, and index creation

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.