Mongodb: Requests

The find is similar to the SELECT statement in MySQL. Used to fetch documents from MongoDB. Returns an array of instruments in the form of the collection if there is no document is an empty collection. Example:
> db.users.find ();
1

> db.users.find ();

Will return all users from the collection.
db.users.find > ({age: 27});
1

db.users.find > ({age: 27});

Will return all users whose age is 27.

You can specify multiple parameters, for example:
db.users.find > ({username: ‘ joe ‘, age: 27});
1

db.users.find > ({username: ‘ joe ‘, age: 27});

It is sometimes necessary to get any specific field of documents. In this case, the query looks like this:
> db.users.find ({}, {username: 1, email: 1});
1

> db.users.find ({}, {username: 1, email: 1});

As a result, we obtain all users only with fields “username” and “email” + “_id” field, which is always the default. If the field is “_id” or some other need not, we can specify the following:
> db.users.find ({}, {username: 1, email: 1, _id: 0});
1

> db.users.find ({}, {username: 1, email: 1, _id: 0});

Requests with the condition

Operators: $lt, $lte is less than less than or equal to, $gt is more $gte is greater than or equal to, is not equal to $ne.

Examples of use:

Get all users whose age is greater than 18 and less than 30
db.users.find > ({age: {$gte: 18, $lte: 30}});
1

db.users.find > ({age: {$gte: 18, $lte: 30}});

Get all of the users ‘ username ‘ not equal to ‘ joe ‘
db.users.find > ({username: {$ne: ‘ joe ‘}});
1

db.users.find > ({username: {$ne: ‘ joe ‘}});

Let’s say we have a collection of stores lottery tickets with the numbers and we need to find only those that have the winning numbers 390, 754, 454. In this case, we use the $in operator:
db.users.find > ({numbers: {$in: [390, 754, 454]}});
1

db.users.find > ({numbers: {$in: [390, 754, 454]}});

the ticket must contain one of these numbers.

Opposite the operator $in is $nin. He, likewise, receives only the tickets with no specified above, rooms.

$Or operator is used in queries when you need to select documents by matching one of the values. For example, we need to select all the tickets with the number 547 or where the “winner” is true:
> db.users.find ({$or: [{tickect_number: 547}, {winner: true}]});
1

> db.users.find ({$or: [{tickect_number: 547}, {winner: true}]});

$Mod operator is used to fetch the keys whose values are divided into the first argument and the remainder is equal to the second parameter. Sounds confusing, that’s better:
db.users.find > ({user_id: {$mod: [5, 1]}});
1

db.users.find > ({user_id: {$mod: [5, 1]}});

This query will return all users “user_id” which is equal to 1, 6, 11, 16, and so on.

To get all the users, in addition to the above, you can use the $not:
db.users.find > ({user_id: {$not, {$mod: [5, 1]}}});
1

db.users.find > ({user_id: {$not, {$mod: [5, 1]}}});

Get users “user_id” 2, 3, 4, 5, 7, 8, 9, 10, 12, and so on.

There is also an operator to check whether there is any key or no-$exist
> db.c.find ({“z”: {“$in”: [null], “$exists”: true}});
1

> db.c.find ({“z”: {“$in”: [null], “$exists”: true}});

So you can select all the documents collection, where the “z” key exists and it is equal to null.
Regular expressions

We all know that regular expressions are very powerful stuff. Mongodb uses a Perl-compatible regular expressions.

Here you can find all users named joe or Joe:
> db.users.find ({“name”:/^ joe $/i});
1

> db.users.find ({“name”:/^ joe $/i});

In General, where there are enough. There are a bunch of services to the same javascript’a, where you can write and test your regul├órki.
Requests within array

Let’s say we have a collection of food and we go there we put the paper with an array of fruits
> db.food.insert ({“fruit”: [“apple”, “banana”, “peach”]});
1

> db.food.insert ({“fruit”: [“apple”, “banana”, “peach”]});

Here’s the query
> db.food.find ({“fruit”, “banana”});
1

> db.food.find ({“fruit”, “banana”});

successfully it will find.

If you want to select documents more than one element of the array, we can use the $all
db.food.find > ({fruits: {$all: [‘ apple ‘, ‘ banana ‘]}});
1

db.food.find > ({fruits: {$all: [‘ apple ‘, ‘ banana ‘]}});

This query will return all the documents in the array of fruits which are apples, and bananas.

Get documents to fully match the elements in an array can:
db.food.find > ({fruits: [“apple”, “banana”, “peach”]});
1

db.food.find > ({fruits: [“apple”, “banana”, “peach”]});

We have a blog, there are comments. We want to get the first 10 comments. To help us comes an $slice:
> db. blog posts.. findOne ({}, {“comments”: {“$slice”: 10}});
1

> db. blog posts.. findOne ({}, {“comments”: {“$slice”: 10}});

find