In the previous article we talked about MongoDB's CRUD basic operations http://blog.csdn.net/stronglyh/article/details/46812579
In this case, let's talk about the advanced--------------of MongoDB.
A: Various inquiries
1: Conditional operator
<, <=, >= This operator does not have to explain much, the most often used is also the simplest.
Db.collection.find ({"field": {$gt: Value}}); Greater than: field > Value
Db.collection.find ({"field": {$lt: Value}}); Less than: Field < value
Db.collection.find ({"field": {$gte: Value}}); Greater than or equal to: field >= value
Db.collection.find ({"field": {$lte: Value}}); Less than equals: field <= value
Suppose you want to satisfy multiple conditions at the same time. Be able to do this
Db.collection.find ({"field": {$gt: value1, $lt: value2}}); value1 < Field < value2
For practice, let's insert a few data.
Db.users.insert ([{name: "Tom", Age:20,sex: "M"},{name: "Lily", Age:40,sex: "F"},{name: "Suby", age:18}]);
2: $all Match all
This operator is similar to the SQL syntax in, but the difference is that in just one value within () can be satisfied, and the $all must satisfy all values within [], such as:
Db.users.find ({age: {$all: [6, 8]}});
Ability to query {name: ' David ', age:26, Age: [6, 8, 9]}
But the query does not show {name: ' David ', age:26, Age: [6, 7, 9]}
3: $exists Infer if the field exists
Querying for data that has field age
> Db.c1.find ({age:{$exists: true});
{"_id": ObjectId ("4fb4a773afa87dc1bed9432d"), "age": +, "Length": 30}
4:null Value Processing
The handling of Null values is slightly odd, and the following example data is detailed:
> Db.c2.find ()
{"_id": ObjectId ("4fc34bb81d8a39f01cc17ef4"), "name": "Lily", "Age": null}
{"_id": ObjectId ("4fc34be01d8a39f01cc17ef5"), "name": "Jacky", "Age": 23}
{"_id": ObjectId ("4fc34c1e1d8a39f01cc17ef6"), "name": "Tom", "addr": 23}
The Age field for "Lily" is empty and Tom does not have an age field. We want to find the line with age empty, in detail such as the following:
> Db.c2.find ({age:null})
{"_id": ObjectId ("4fc34bb81d8a39f01cc17ef4"), "name": "Lily", "Age": null}
{"_id": ObjectId ("4fc34c1e1d8a39f01cc17ef6"), "name": "Tom", "addr": 23}
Oddly enough, we thought we could only find Lily, but "Tom" was also found, because "null" not only matches a document with a key value of NULL, but also matches a document that does not include the key. So how can talent just find "Lily"? We use exists to limit it.
5: $mod modulo operation
Query age modulo 10 equals 0 of data
Db.student.find ({age: {$mod: [10, 1]}})
The proportions are as follows:
The data for the C1 table is as follows:
Db.c1.find ()
{"_id": ObjectId ("4fb4af85afa87dc1bed94330"), "age": 7, "length_1": 30}
{"_id": ObjectId ("4fb4af89afa87dc1bed94331"), "age": 8, "length_1": 30}
{"_id": ObjectId ("4fb4af8cafa87dc1bed94332"), "age": 6, "length_1": 30}
Query age modulo 6 equals 1 of data
> Db.c1.find ({age: {$mod: [6, 1]}})
{"_id": ObjectId ("4fb4af85afa87dc1bed94330"), "age": 7, "length_1": 30}
Can see that only shows the age modulo 6 equals 1 of the data, other non-conforming data is not displayed
6: $ne Not equal to
The value of the query x is not equal to 3 of the data
Db.things.find ({x: {$ne: 3}});
The proportions are as follows:
The data for the C1 table is as follows:
> Db.c1.find ()
{"_id": ObjectId ("4fb4af85afa87dc1bed94330"), "age": 7, "length_1": 30}
{"_id": ObjectId ("4fb4af89afa87dc1bed94331"), "age": 8, "length_1": 30}
{"_id": ObjectId ("4fb4af8cafa87dc1bed94332"), "age": 6, "length_1": 30}
Query the value of age not equal to 7 of data
> Db.c1.find ({age: {$ne: 7}});
{"_id": ObjectId ("4fb4af89afa87dc1bed94331"), "age": 8, "length_1": 30}
{"_id": ObjectId ("4fb4af8cafa87dc1bed94332"), "age": 6, "length_1": 30}
Can see that only the age does not equal 7 of the data, and other non-conforming data is not shown
7: $in including
Is the same as the purpose of SQL standard syntax, which is to query a range of enumerated values
Querying the value of x within the 2,4,6 range of data
Db.things.find ({x:{$in: [2,4,6]}});
The proportions are as follows:
The data for the C1 table is as follows:
> Db.c1.find ()
{"_id": ObjectId ("4fb4af85afa87dc1bed94330"), "age": 7, "length_1": 30}
{"_id": ObjectId ("4fb4af89afa87dc1bed94331"), "age": 8, "length_1": 30}
{"_id": ObjectId ("4fb4af8cafa87dc1bed94332"), "age": 6, "length_1": 30}
Querying for the value of age within the 7,8 range of data
> Db.c1.find ({age:{$in: [7,8]}});
{"_id": ObjectId ("4fb4af85afa87dc1bed94330"), "age": 7, "length_1": 30}
{"_id": ObjectId ("4fb4af89afa87dc1bed94331"), "age": 8, "length_1": 30}
Can see that only the age equals 7 or 8 of the data is shown, and other non-conforming data is not shown
The $in is flexible enough to specify different types of conditions and values.
8: $nin not included
Is the same as the purpose of SQL standard syntax, that is, the data to be queried is outside the range of a series of enumerated values
Querying the value of x outside the 2,4,6 range of data
Db.things.find ({x:{$nin: [2,4,6]}});
The proportions are as follows:
The data for the C1 table is as follows:
> Db.c1.find ()
{"_id": ObjectId ("4fb4af85afa87dc1bed94330"), "age": 7, "length_1": 30}
{"_id": ObjectId ("4fb4af89afa87dc1bed94331"), "age": 8, "length_1": 30}
{"_id": ObjectId ("4fb4af8cafa87dc1bed94332"), "age": 6, "length_1": 30}
Querying for the value of age outside the range of 7,8 data
> Db.c1.find ({age:{$nin: [7,8]}});
{"_id": ObjectId ("4fb4af8cafa87dc1bed94332"), "age": 6, "length_1": 30}
Can see that only the age does not equal 7 or 8 of the data, and other non-conforming data is not displayed
9: Number of $size array elements
For {name: ' David ', age:26, Favorite_number: [6, 7, 9]} record
Match Db.users.find ({favorite_number: {$size: 3}});
Mismatch Db.users.find ({favorite_number: {$size: 2}});
The "$size" is also useful for querying arrays. Name implies. It can be used to query an array of a specific length.
Like what:
> Db.users.find ({favorite_number: {$size: 3}})
Getting a document within a length range is a common query. $size cannot be used in combination with other query conditions, such as "$GT". However, this query can be implemented by adding a "size" key to the document. This adds the value of "size" at the same time every time the element is added to the specified array. For example, the original update:
> db.users.update (criteria,{"$push": {"Favorite_number": "1"}})
It will become the following:
> Db.users.update (Criteria,
... {"$push": {"Favorite_number": "1"}, "$inc": {"$size": 1}})
The self-increment operation is very fast. So the impact on performance is minimal.
This stores the document later. You can query like this:
> Db.users.find ({"$size": {"$GT": 3}})
Very sorry. This technique cannot be used at the same time as the "$addToSet" operator.
10: Regular Table-matching
Query mismatch name=b* lead record
Db.users.find ({name: {$not:/^b.*/}});
The proportions are as follows:
The data for the C1 table is as follows:
> Db.c1.find ();
{"_id": ObjectId ("4fb5faaf6d0f9d8ea3fc91a8"), "name": "Tony", "Age": 20}
{"_id": ObjectId ("4fb5fab96d0f9d8ea3fc91a9"), "name": "Joe", "Age": 10}
11:javascript queries and $where queries
Query a more than 3 of the data, the following query method the same way
Db.c1.find ({A: {$gt: 3}});
Db.c1.find ({$where: "THIS.A > 3"});
Db.c1.find ("THIS.A > 3");
f = function () {return THIS.A > 3;} db.c1.find (f);
12:count query record number of lines
Count Query Record bars
Db.users.find (). Count ();
The following return is not 5, but the total number of records in the user table
Db.users.find (). Skip (. Limit (5). Count ();
Suppose you want to return the number of records after the limit. To use COUNT (true) or count (not 0)
Db.users.find (). Skip (. Limit (5). Count (True);
The proportions are as follows:
The data for the C1 table is as follows:
> Db.c1.find ()
{"_id": ObjectId ("4fb5faaf6d0f9d8ea3fc91a8"), "name": "Tony", "Age": 20}
{"_id": ObjectId ("4fb5fab96d0f9d8ea3fc91a9"), "name": "Joe", "Age": 10}
Querying the amount of data in a C1 table
> Db.c1.count ()
2
To see that the table has 2 data in common.
Two: Cursors
The 1:find command does not directly return the result, but instead returns an iterator to the result set, the cursor.
Like most database products. MongoDB also uses cursors to cycle through each result data, with detailed syntax as follows:
> for (var c = Db.t3.find (); C.hasnext ();) {
... Printjson (C.next ());
... }
{"_id": ObjectId ("4fb8e4838b2cb86417c9423a"), "Age": 1}
{"_id": ObjectId ("4fb8e4878b2cb86417c9423b"), "Age": 2}
{"_id": ObjectId ("4fb8e4898b2cb86417c9423c"), "Age": 3}
{"_id": ObjectId ("4fb8e48c8b2cb86417c9423d"), "Age": 4}
{"_id": ObjectId ("4fb8e48e8b2cb86417c9423e"), "Age": 5}
2:mongodb There is also a way to handle cursors
> Db.t3.find (). ForEach (function (u) {printjson (U);});
{"_id": ObjectId ("4fb8e4838b2cb86417c9423a"), "Age": 1}
{"_id": ObjectId ("4fb8e4878b2cb86417c9423b"), "Age": 2}
{"_id": ObjectId ("4fb8e4898b2cb86417c9423c"), "Age": 3}
{"_id": ObjectId ("4fb8e48c8b2cb86417c9423d"), "Age": 4}
3:skip limit the starting point for returning records
Starting from 3rd record, return 5 records (limit 3, 5)
Db.users.find (). Skip (3). Limit (5);
The proportions are as follows:
The data for the C1 table is as follows:
> Db.c1.find ()
{"_id": ObjectId ("4fb5faaf6d0f9d8ea3fc91a8"), "name": "Tony", "Age": 20}
{"_id": ObjectId ("4fb5fab96d0f9d8ea3fc91a9"), "name": "Joe", "Age": 10}
4:sort sort
With age ascending ASC
Db.users.find (). Sort ({age:1});
Descending desc by age
Db.users.find (). Sort ({Age:-1});
The data for the C1 table is as follows:
> Db.c1.find ()
{"_id": ObjectId ("4fb5faaf6d0f9d8ea3fc91a8"), "name": "Tony", "Age": 20}
{"_id": ObjectId ("4fb5fab96d0f9d8ea3fc91a9"), "name": "Joe", "Age": 10}
Query C1 table in ascending order of age
> Db.c1.find (). Sort ({age:1});
{"_id": ObjectId ("4fb5fab96d0f9d8ea3fc91a9"), "name": "Joe", "Age": 10}
{"_id": ObjectId ("4fb5faaf6d0f9d8ea3fc91a8"), "name": "Tony", "Age": 20}
The 1th one is age=10, and then the result set is sorted in ascending order
Three: Stored Procedures
The stored procedure of a relational database is described as a set of SQL statements that have been compiled and stored in a database in order to complete a specific function. The user runs it by specifying the name of the stored procedure and giving the number of parameters (assuming that the stored procedure has a reference).
MongoDB provides a number of solutions to a number of problems, and it is still not showing any weakness and performance in other database features.
MongoDB supports stored procedures in the same way, but MongoDB is written in JavaScript. This is the charm of MongoDB.
1:sql self-defined functions into MongoDB stored procedures
MongoDB supports stored procedures in the same way. The first thing you need to know about a stored procedure is that it's written in JavaScript. Maybe it makes you wonder why it's written in JavaScript, but it actually makes you very comfortable, and the MongoDB stored procedure is stored in the Db.system.js table. Let's imagine a simple SQL self-defining function such as the following:
function AddNumbers (x, y) {
return x + y;
}
Here we convert this SQL-defined function into MongoDB's stored procedure:
> Db.system.js.save ({_id: "AddNumbers", Value:function (x, y) {return x + y;}});:
2: Querying Stored Procedures
Stored procedures can be viewed, changed, and deleted, so we use find to see if the stored procedure has been created.
> Db.system.js.find ()
{"_id": "AddNumbers", "value": Function cf__1__f_ (x, y) {
return x + y;
} }
>
3:eval function
The front looks good, and here I look at the actual call to this stored procedure:
> Db.eval (' AddNumbers (3, 4.2) ');
7.2
>
This method of operation is simply too simple, perhaps this is the charm of MongoDB.
Db.eval () is a strange thing, and we are able to call the logic of the stored procedure directly inside and at the same time without having to declare the logic of the stored procedure in advance.
> Db.eval (function () {return 3+3;});
6
>
As can be seen from the above, MongoDB's stored procedures can easily complete the arithmetic operation, but other database products in the stored procedure can handle some things inside the database. such as the amount of data to take out a table and so on, these MongoDB can do it? The answer is yes. MongoDB can easily do, see the following examples:
> Db.system.js.save ({_id: "get_count", Value:function () {return Db.c1.count ();}});
> Db.eval (' get_count () ')
2
The ability to see stored procedures easily manipulate tables in stored procedures
Thanks for http://hubwiz.com/'s support.
Advanced query of MongoDB step one