MongoDB Common Commands Summary

Source: Internet
Author: User

Learn a little bit every day, progress a little, record a little, share a little, summarize a little.

First, the basic application

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 and add

Db.users.save ({"Name": "LECAF"}) created a collection named users and added a new {"Name": "LECAF"} data

Db.users.insert ({"Name": "Ghost", "Age": 10}) Inserts a new piece of 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 operation

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": 10}) modifies the NAME=LECAF data to age=10, the first parameter is the lookup condition, the second parameter is the modification, except the primary key, the other content is replaced by the contents of the second parameter , the primary key cannot be modified,

Second, 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

Note: Follow-up is to summarize some actual project experience come out.

learning always requires a process, and accumulation also requires a process. Love program love life.

MongoDB Common Commands Summary

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.