MongoDB Tutorial Lesson 18th mongodb Common Command Database command Collection Operations Command

Source: Internet
Author: User
Tags eval mongodb tutorial

Document-oriented NoSQL database mainly solves the problem is not high-performance concurrent read-write, but to ensure the massive data storage at the same time, with good query performance.

conditional operator

&lt, <=, >= This operator does not have to explain more, the most common is 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 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 one value of () is satisfied, and the $all must be

All values within [] must be met, for example: Db.users.find ({age: {$all: [6, 8]}}); Can query {name: ' David ', age:26, Age: [6, 8, 9]} but cannot query {name: ' David ', age:26, Age: [6, 7, 9]}

$exists determine if a field exists

Queries all records that exist in the age field Db.users.find ({age: {$exists: true}}); Query all records that do not exist in the Name field Db.users.find ({name: {$exists: false}});

Null Value Handling

> Db.c2.find ({age:null})

$mod modulo operations

Query age modulo 6 equals 1 of data

Db.c1.find ({age: {$mod: [6, 1]}})

$ne Not equal to

The value of the query x is not equal to 3 of the data Db.c1.find ({age: {$ne: 7}});

$in contains

Db.c1.find ({age:{$in: [7,8]}});

$nin does not contain

Querying for the value of age outside the range of 7,8 data

Db.c1.find ({age:{$nin: [7,8]}});

$size number of array elements

For {name: ' David ', age:26, Favorite_number: [6, 7, 9]} records match Db.users.find ({favorite_number: {$size: 3}});

Mismatch Db.users.find ({favorite_number: {$size: 2}});

Regular expression matching

Query for data whose name does not start with T

Db.c1.find ({name: {$not:/^t.*/}});

Javascript queries and $where queries

Querying a data greater than 3, the following query method is the same as 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 Query Record bars

Db.users.find (). Count (); The following return is not 5, but is the number of records in the user Table Db.users.find (). Skip (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 (. Limit (5). Count (True);

Skip limit Returns the start of a record

Starting from 3rd record, return 5 records (limit 3, 5)

Db.users.find (). Skip (3). Limit (5);

Sort sorts

With age ascending ASC

Db.users.find (). Sort ({age:1}); With age descending desc db.users.find (). Sort ({Age:-1});

Cursor

for (var C = Db.t3.find (); C.hasnext ();) {

Printjson (C.next ());}

MongoDB also has another way to handle cursors

> Db.t3.find (). ForEach (function (u) {printjson (U);});

Stored Procedures

The first thing you need to know about stored procedures is that it's written in JavaScript.

MongoDB stored procedures are stored in the Db.system.js table, we imagine a simple SQL Custom function as follows:

function AddNumbers (x, y) {

return x + y; }

Here we convert this SQL custom function to MongoDB's stored procedure:

> Db.system.js.save ({_id: "AddNumbers", Value:function (x, y) {return x + y;}});

Stored procedures can be viewed, modified, and deleted, so we use find to see if the stored procedure has been created. > Db.system.js.find () Call this stored procedure:

Db.eval (' AddNumbers (3, 4.2) ');

Db.eval () is a strange thing, and we can call the logic of the stored procedure directly inside and at the same time without having to declare the logic of the stored procedure beforehand.

Db.eval (function () {return 3+3;});

MongoDB Tutorial Lesson 18th mongodb Common Command Database command Collection 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.