MongoDB Installation Test

Source: Internet
Author: User

About MongoDB, not much to say, I would also like to install the experience.

MongoDB Installation

I installed the version: mongodb-win32-i386-2.6.7

1, set up the folder in the D disk d:\mogondb, create folders d:\mongodb\data\db, D:\mongodb\data\log, respectively, to install the DB and log files, in the log folder to create a log file MongoDB.log, that is, D : \mongodb\data\log\mongodb.log

2, unzip the installation package, the Bin folder and the same level of content was just in D:\mongodb

3, run Cmd.exe into the DOS command interface, execute the following command

> D:

> CD D:\mongodb\bin

> D:\mongodb\bin>mongod-dbpath "D:\mongodb\data\db"

Then type "http://localhost:27017/" inside the browser, which appears

"It looks like you were trying to access MongoDB over HTTP on the native driver port."

Success.

4. MongoDB is installed as a Windows service

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

Here MongoDB.log is the log file that was started,--servicename the "MongoDB" service named MongoDB

Then start the MongoDB service

5.

Shutting down services and deleting processes

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

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

MongoDB Common operations

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 into the Users collection, and if there is no users this collection, MongoDB automatically creates
    • There is a slight difference between save () and insert (): If the new data primary key already exists, insert () will not do the operation and prompt the error, while save () changes the original content to 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 the N1 to N2, with the function of update.

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 modified content, except the primary key, Other content will be replaced by the contents of the second parameter, the primary key cannot be modified,

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: [10, 1]}}) modulo operation, equal to key% 10 = = 1 i.e. key divided by 10 + 1
  • Db.collection.find ({"key": {$nin: [1, 2, 3]}) does not belong, the condition equals the value of key does not belong to either [1, 2, 3]
  • Db.collection.find ({"key": {$in: [1, 2, 3]}), the condition equals key equal to either [1, 2, 3]
  • Db.collection.find ({"key": {$size: 1}}) $size number, size, condition equal to the number of values of key is 1 (key must be an array, a value can not be counted as an array of 1)
  • Db.collection.find ({"key": {$exists: True|false}}) $exists field exists, true returns data with field key, FALSE returns data with no Word key
  • 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 versions are available), eligible a=1 or eligible b=2 data will be queried
  • Db.collection.find ({"Key": Value, $or: [{a:1}, {b:2}]}) meets the criteria Key=value and matches any of the other two criteria
  • 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, the limit () will not work if it is not constrained
    • Db.collection.find (). Skip (5) controls how much the returned result skips, and if the argument is 0, then skip () will not work, or skip 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) to obtain the actual number of results returned when adding skip () and limit (), a parameter of true is required. Otherwise, the total number of results that match the query criteria is returned

MongoDB Installation Test

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.