Brother Lian Learning python-windows under MongoDB installation and use finishing

Source: Internet
Author: User

first, to install MongoDB

1.:http://www.mongodb.org/downloads

2. unzip to the directory you want to install, such as D:\mongodb

3. Create a folder

D:\mongodb\data\db,d:\mongodb\data\log, respectively, for installing db and log files , log folder to create a journal file MongoDB.log, which is D:\mongodb\data\log\MongoDB.log

    1. run cmd.exe into the dos command interface and execute the following command

>d:

  > CD Mongodb\bin

  > D:\mongodb\bin\mongod.exe--dbpath D:\mongodb\data\db

see a similar message that the start mongodb service succeeds, the default mongodb listener port is 27017,MySQL is 3306 .

Browser address: localhost:27017

5. Test the connection

open a new cmd window, enter the mongodb bin directory, enter MONGO or Mongo.exe , the following message appears indicating that the test passed, at which point we have entered the Test This database, how to enter the other database below will say.

 

Enter exit or Ctrl + C to exit.

6. Configuring the Windows service for MongoDB

when Mongod.exe is turned off,mongo.exe cannot connect to the database, so each time you want to use a mongodb database, you must open mongod.exe program, so it's more troublesome, we can install MongoDB as windows Service

or run cmd, go to the bin folder and execute the following command

E:\mongodb\bin>mongod.exe--dbpath E:\mongodb\data\db--logpath E:\mongodb\data\log\MongoDB.log--install-- ServiceName MongoDB

  MongoDB.log is the log file that is started to be created,

--servicename "MongoDB" service named MongoDB

 (a) start mongodb service

 > d:\mongodb\bin>net start mongodb (lowercase can also:NET START mongodb)

 

Open Task Manager and you can see that the process has started

(b) Close Service

> D:\mongodb\bin>net Stop MongoDB ( close service )

(c) Delete a process

> D:\mongodb\bin>mongod--dbpath "d:\mongodb\data\db"--logpath "D:\mongodb\data\log\MongoDB.log"--remove-- ServiceName "MongoDB"

( Delete, note not --install .)

ii. use of MongoDB

1. Common Commands

    • Show DBS display database list
    • Use dbname into the dbname database, case sensitive, no this database does not matter
    • Show Collections displays a collection in the database, equivalent to a table

2. Create & Add

  • Db.users.save ({"Name": "LECAF"}) created a collection named users and added a new {"name": "LECAF"} data
  • Db.users.insert ({"Name": "Ghost", "Age": ten}) Inserts a new data in the Users collection, if there is no users this collection, MongoDB will automatically create
  • Save ()and theInsert ()There is a slight difference: if the new data primary key already exists,Insert ()will not do the operation and prompt the error, andSave ()change the original content to the new content.
      • Existing data: {_id:1, "name": "N1"} , _id is the primary key
      • Insert ({_id:1, "name": "N2"}) will prompt for an error
      • Save ({_id:1, "name": "N2"}) will change N1 to n2 , with u the role of pdate.

3. Delete

    • Db.users.remove () Delete all data under the Users collection
    • Db.users.remove ({"Name": "LECAF"}) deletes data NAME=LECAF under the users collection
    • Db.users.drop () or db.runcommand ({"Drop", "users"}) Delete collection users
    • Db.runcommand ({"Dropdatabase": 1}) Delete the current database

4. Find

    • Db.users.find () find all data in the Users collection
    • Db.users.findOne () to find the first data in the Users collection

5. Modifications

    • Db.users.update ({"Name": "LECAF"}, {"Age": ten}) modifies the NAME=LECAF data to age=10, The first parameter is the lookup condition, the second parameter is the modification, in addition to the primary key, the other content will be replaced by the contents of the second parameter, the primary key cannot be modified,

 

Third, advanced applications

1. Condition Lookup

  • Db.collection.find ({"Key": value}) find data for Key=value
  • Db.collection.find ({"key": {$gt: Value}}) key > value
  • Db.collection.find ({"key": {$lt: Value}}) key < value
  • Db.collection.find ({"key": {$gte: Value}}) key >= value
  • Db.collection.find ({"key": {$lte: Value}}) key <= value
  • Db.collection.find ({"key": {$gt: value1, $lt: value2}}) value1 < key <value2
  • Db.collection.find ({"key": {$ne: Value}}) key <> value
  • Db.collection.find ({"key": {$mod: [1]}}) modulo operation, equivalent to key% = = 1 i.e. key c4> divided by a remainder of 1.
  • Db.collection.find ({"key": {$nin: [1, 2, 3]}) does not belong, the condition equal to the value of key does not belong to [1, 2, 3]< any one of the /c12>
  • Db.collection.find ({"key": {$in: [1, 2, 3]}) , the condition equals key equal to [1, 2, 3] in any What one
  • Db.collection.find ({"key": {$size: 1}}) $size number, size, and condition equal to the number of values of key is 1( C10>key must be an array, the case of a value cannot be counted as An array of 1)
  • Db.collection.find ({"key": {$exists: True|false}}) $exists field exists,true returns the existence field key of data, false returns the non-existent word degree Key of Data
  • Db.collection.find ({"Key":/^val.*val$/i}) Regular, like ;"i" ignores case, "M" supports multiple lines
  • Db.collection.find ({$or: [{a:1}, {b:2}]}) $or or (note:MongoDB 1.5.3 later version is available), eligible a=1 or qualifying b=2 data will be queried.
  • Db.collection.find ({"Key": Value, $or: [{a:1}, {b:2}]}) meets the criteria key=value , while complying with any of the other two conditions One of the data
  • Db.collection.find ({"Key.subkey": Value}) The values in the inline object match, note:"Key.subkey" must be quoted
  • Db.collection.find ({"key": {$not:/^val.*val$/i}}) this is an operator that is used in combination with other query conditions and is not used alone. The result set obtained by the above query condition plus $not can get the opposite set.

2. sorting

    • Db.collection.find (). Sort ({"Key1":-1, "Key2": 1}) here the 1 stands for Ascending,-1 for descending

3. other

  • Db.collection.find (). Limit (5) controls the number of returned results, and if the argument is 0, thelimit () will not work if it isnot constrained
  • Db.collection.find (). Skip (5) controls how many amounts are skipped for the returned result, and if the argument is 0,Skip () will not work if the parameter is not constrained. or skipped 0 .
  • Db.collection.find (). Skip (5). Limit (5) can be used for paging, skipping 5 data and then fetching 5 data
  • Db.collection.find (). Count (True) count () returns the number of result set bars
  • Db.collection.find (). Skip (5). Limit (5). Count (True) at join Skip () and limit () In both operations, a parameter of true is required to obtain the actual number of results returned, Otherwise the total number of results that match the query criteria is returned.

Brother Lian Learning python-windows under MongoDB installation and use finishing

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.