The document-oriented nosql database does not solve high-performance concurrent read/write, but ensures massive data storage and good query performance.
Conditional Operators
<, <=,>,> = This operator does not need to be explained. The most common operator is the simplest database. 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 or equal to: Field <= value if you want to satisfy multiple conditions at the same time, you can do this
DB. collection. Find ({"field": {$ GT: value1, $ LT: value2}); // value1 <field <Value
$ All match all
This operator is similar to the SQL syntax in, but the difference is that in only needs to satisfy a value in (), and $ all must
All values in [] must be satisfied, such as DB. users. find ({age :{$ All: [6, 8]}); you can query {Name: 'David ', age: 26, age: [6, 8, 9]} but no {Name: 'David ', age: 26, age: [6, 7, 9]}
$ Exists
Query all records with the age field. users. find ({age :{$ exists: true}); query all records without the name field. users. find ({name :{$ exists: false }});
Null Value Processing
> DB. c2.find ({age: NULL })
$ Mod modulo operation
Query the data of age modulo 6 equal to 1
DB. c1.find ({age: {$ mod: [6, 1]})
$ Ne is not equal
Query data dB. c1.find ({age :{$ ne: 7 }}) whose x value is not 3 }});
$ In inclusion
DB. c1.find ({age: {$ in: []});
$ Nin does not contain
Query data with age values out of the range of 7 or 8.
DB. c1.find ({age: {$ Nin: []});
$ Size: Number of array elements
For {Name: 'David ', age: 26, favorite_number: [6, 7, 9]}, record match dB. users. find ({favorite_number: {$ size: 3 }});
Does not match dB. Users. Find ({favorite_number: {$ size: 2 }});
Regular Expression matching
Query data whose name does not start with T
DB. c1.find ({Name: {$ not:/^ t .*/}});
Javascript query and $ where Query
To query data greater than 3, the following query method is similar to 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 );
Count
DB. users. find (). count (); The following does not return 5, but the number of all records in the User table dB. users. find (). skip (10 ). limit (5 ). count (); if you want to return the number of records after the limit, use count (true) or count (not 0)
DB. Users. Find (). Skip (10). Limit (5). Count (true );
Skip limit the start point of the returned record
Returns 5 Records (limit 3, 5) starting from 3rd records)
DB. Users. Find (). Skip (3). Limit (5 );
Sort sorting
ASC in ascending order of age
DB. Users. Find (). Sort ({age: 1}); DESC dB. Users. Find (). Sort ({age:-1}) in descending order of age });
Cursor
For (VAR c = dB. t3.find (); C. hasnext ();){
Printjson (C. Next ());}
MongoDB has another way to process the cursor.
> DB. t3.find (). foreach (function (u) {printjson (U );});
Stored Procedure
The first thing you need to know about a stored procedure is that it is written in JavaScript.
The MongoDB stored procedure is stored in the DB. system. js table. We imagine a simple SQL custom function as follows:
Function addnumbers (x, y ){
Return X + Y ;}
Next we will convert this SQL user-defined function to the MongoDB storage process:
> DB. system. js. Save ({_ ID: "addnumbers", value: function (x, y) {return x + y ;}});
The stored procedure can be viewed, modified, and deleted. Therefore, we can use find to check whether the stored procedure has been created. > DB. system. js. Find:
DB. eval ('addnumbers (3, 4.2 )');
DB. eval () is a strange thing. We can directly store the Stored Procedure logic and call it at the same time without declaring the Stored Procedure logic in advance.
DB. eval (function () {return 3 + 3 ;});