MongoDB Basic Operations Command

Source: Internet
Author: User
Tags findone

Due to work needs, the author has used MongoDB for two days. Really not used to it! But the commands are much simpler than MySQL and SQL Server. Here to tidy up some MongoDB basic operation commands to share.

Client installation is not said, the author is the shell through the server connected to the MongoDB operation, directly into the topic.

Show DBS: Display Database list
Show Collections: Displays a collection in the current database (similar to a table in a relational database)
Show Users: Show user

Use <db name>: Switch the current database, which is the same as the meaning in Ms-sql
Db.help (): Show database Operations Command with a lot of commands
Db.foo.help (): Displays the set Operation command, as well as a lot of commands, Foo refers to the current database, a collection called Foo, not the real meaning of the command
Db.foo.find (): Data lookup for the Foo collection in the current database (all data is listed because there are no conditions)
Db.foo.find ({a:1}): Looks for the Foo collection in the current database, provided the data has a property called a, and A has a value of 1

MongoDB does not have a command to create a database, but there are similar commands.

For example, if you want to create a "myTest" database, run the use myTest command first, then do something (such as: db.createcollection (' user ')) so that you can create a database called "MyTest".

Because the author is a test, so the current use does not increase, delete, change. But I remember in mon that the corresponding command should be insert, drop, update. These are only for table data. There is no contact between the database and the data table. The following is a condition query command for Mon.

Find ()/findone ()
The query operation of the MONGODB database uses the Find () or FindOne () function, or it can be queried according to different criteria. The wording of the query (in Find (), for example) can be as follows:
Db. A.find ()/db. A.find ({})

The "{}" omission does not affect the query, which means finding all the documents under collection A. You can also make a document conditional: DB. A.find ({"A": 1, "B": 1}), which finds the same
A document that is equal to 1 and attribute B equals 1, and if it needs to satisfy the attribute C equals 1, can be added directly to the document: DB. A.find ({"A": 1, "B": 1, "C": 1}).

Specify settings for keys
The setting of the specified key is displayed, such as a set has 10 attributes, after the query only care about a few properties of the document in the collection. The following documents:
{"_id": ObjectId ("5018da521781352fe25bf4d2"), "a": "1", "B": "1", "C": "1", "D": "1", "E": "1"}
The relationship-only property a,b,c can be set as follows:
Db. A.find ({},{"A,": 1, "B": 1, "C":, "_id": 0})
The 1 and 0 here are different from the key values in the document, where 1: Represents the display, and 0: Indicates that it is not displayed. Where the "_id" key is present by default, you need to display the settings.

The results are as follows:
{"A": "1", "B": "1", "C": "1"}

--------------------------------------conditional queries can be analogous to Structured query statements SQL--------------------------------------------
Conditional query
Conditional operator
"$lt" ===================> "<"
"$lte" ==================> "<="
"$gt" ===================> ">"
"$gte" ==================> ">="
"$ne" ===================> "! ="

Such as: A set of B set of Chinese documents have a property x value is an integer, you need to find the 10<x<=30 document, the following wording:
Db. B.find ({"x": {"$GT": Ten, "$lte": 30}})

For example, find the Date property from a collection B with a day value greater than the 2012/01/01 document data, as follows:
Db. B.find ({"Day": {"$GT": New Date ("2012/01/01")}})
It is suitable for the business processing scope such as daily scheduling, monthly dispatching, weekly dispatching data, etc.

$in contains/$nin does not contain
$in: Query the document that matches the specified condition value;
$nin: The query does not match the document for the specified condition value;

SQL: Syntax: field in (' Value 1 ', ' value 1 ' ...)
Mongodb:db. B.find ({"x": {"$in": [' value 1 ', ' Value 2 ',.....]}})

SQL: Syntax: Field not IN (' Value 1 ', ' value 1 ' ...)
Mongodb:db. B.find ({"x": {"$nin": [' value 1 ', ' Value 2 ',.....]}})

$in/$nin Advantages: You can specify different types of conditions and values.

$or or query

$or: Query a document that matches multiple values of multiple criteria;

SQL: syntax: Field 1 = ' xxx ' or field 2 in (' xxx ') .....
Mongodb:db. B.find ({"$or": [{"x": {"$in": [' value 1 ', ' Value 2 ' ...]}},{"y": "3"}]})

$all Match All

such as documentation:
{"Name": Jack, "age": [All-in-all]}
{"Name": Jack, "age": [1,4,3]}

Db. B.find ({"Age": {"$all": [2,3]}}) Result: {"name": Jack, "age": [All-in-all]}

$exists determine if a document property exists

Db. B.find ({"name": {"$exists": true})--Find the document where the attribute name exists
Db. B.find ({"name": {"$exists": false}})--Find a document with a property name that does not exist

Property value is null case
Here are some things to know:
> DB. C.find ()
{"_id": ObjectId ("5018fccd1781352fe25bf511"), "a": "+", "B": "14"}
{"_id": ObjectId ("5018fccd1781352fe25bf512"), "a": "" "," B ":" 15 "}
{"_id": ObjectId ("5018fccd1781352fe25bf510"), "a": "+", "B": "+", "C": null}
> DB. C.find ({"c": null})
{"_id": ObjectId ("5018fccd1781352fe25bf511"), "a": "+", "B": "14"}
{"_id": ObjectId ("5018fccd1781352fe25bf512"), "a": "" "," B ":" 15 "}
{"_id": ObjectId ("5018fccd1781352fe25bf510"), "a": "+", "B": "+", "C": null}
The Visible query property c value is a null document, including the attribute C value is null, and the attribute C does not exist in two parts. If you want to query only documents with property C as null
As follows:
> DB. C.find ({"C": {"$in": [null], "$exists": True}})
{"_id": ObjectId ("5018fccd1781352fe25bf510"), "a": "+", "B": "+", "C": null}

$not-element conditional clauses

Can be used in conjunction with other conditions, that is, a document that is not within the matching range, and its usage is visible below.

$mod modulo operations

Db. B.find ({"Age": {"$mod": [5,1]}})--represents all documents looking for ages/5 + 1

If you look for all documents that are older than 5 + 1, combine $not operations:
Db. B.find ({"Age": {"$not": {"$mod": [5,1]}}})

Regular expressions

Db. B.find ({"Name":/jack/i})

$size

> DB. C.find ()
{"_id": ObjectId ("501e71557d4bd700257d8a41"), "a": "1", "B": [1, 2, 3]}
{"_id": ObjectId ("501e71607d4bd700257d8a42"), "a": "1", "B": [1, 2]}
> DB. C.find ({"B": {"$size": 2}})
{"_id": ObjectId ("501e71607d4bd700257d8a42"), "a": "1", "B": [1, 2]}

$slice

Returns a subset of the array, that is, how many bars (ranges) are returned based on a property. You can also accept the offset value and the number of elements to return to return the intermediate result.
> DB. C.find ()
{"_id": ObjectId ("501e71557d4bd700257d8a41"), "a": "1", "B": [1, 2, 3]}
{"_id": ObjectId ("501e71607d4bd700257d8a42"), "a": "1", "B": [1, 2]}
> DB. C.findone ({},{"B": {"$slice": [2,3]}})
{"_id": ObjectId ("501e71557d4bd700257d8a41"), "a": "1", "B": [3]}
> DB. C.findone ({},{"B": {"$slice":-2}})
{
"_id": ObjectId ("501e71557d4bd700257d8a41"),
"A": "1",
"B": [
2,
3
]
}

$where

You can execute the task JavaScript as part of the query.
The value of the $where can be a function, a string, and so on.

Db. C.find ({"$where": function () {return THIS.A = = "1"}}) and DB. C.find ({"$where": "THIS.A = = ' 1 '"}})

Note: Queries with $where clauses are much slower than regular queries in speed. Because the document needs to be converted from Bson to a JavaScript object, it is then run through an expression of "$where".
Do not use indexes. Regular queries can be used to do pre-filtering, configure the "$where" query for tuning, can achieve without sacrificing performance requirements.

As for the multi-table query, there is no use, and later study slowly. Conditional query to here basically also satisfies my use. I hope we can help you.

MongoDB Basic Operations Command

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.