MongoDB Common operations

Source: Internet
Author: User
Tags mongodb

Find method

Db.collection_name.find ();

Query all the results:

select * from Users;

Db.users.find ();

Specify which columns to return (key):

Select name, skills from users;

Db.users.find ({}, {' name ': 1, ' Skills ': 1});

Supplemental Note: The first {} places a Where condition the second {} Specifies that the columns are displayed and not displayed (0 indicates that no 1 is displayed)

Where Condition:

1. Simple equal To:

Select name, age, skills from users where name = ' hurry ';

Db.users.find ({' name ': ' Hurry '},{' name ': 1, ' age ': 1, ' Skills ': 1});

2. Using and

Select name, age, skills from users where name = ' hurry ' and age = 18;

Db.users.find ({' name ': ' Hurry ', ' age ': 18},{' name ': 1, ' age ': 1, ' Skills ': 1});

3. Use or

Select name, age, skills from users where name = ' hurry ' or age = 18;

Db.users.find ({' $or ': [{' name ': ' Hurry '}, {' Age ':}]},{' name ': 1, ' age ': 1, ' Skills ': 1});

4.<, <=, >= ($lt, $lte, $GT, $gte)

SELECT * from the users where age >= <= 30;

Db.users.find ({' age ': {' $gte ': ' $lte ': 30}});

5. In ($in, $nin) using in

SELECT * from the users where age in (10, 22, 26);

Db.users.find ({' age ': {' $in ': [10, 22, 26]}});

6. Matching NULL

SELECT * from users where-age is null;

Db.users.find ({' age ': null);

7.like (MongoDB supports regular expressions)

SELECT * from the users where name like "%hurry%";

Db.users.find ({name:/hurry/});

SELECT * from the users where name like "hurry%";

Db.users.find ({name:/^hurry/});

8. Use of distinct

Select DISTINCT (name) from users;

Db.users.distinct (' name ');

9. Use Count

Select COUNT (*) from users;

Db.users.count ();

10. Array query (MongoDB own special)

If skills is [' java ', ' Python ']

Db.users.find ({' Skills ': ' java '}); The statement can match a successful

$all

Db.users.find ({' skills ': {' $all ': [' java ', ' Python ']}}) skills must contain both Java and Python

$size

Db.users.find ({' skills ': {' $size ': 2}}) Unfortunately $size cannot be used in combination with $LT, etc.

$slice

Db.users.find ({' skills ': {' $slice: [1,1]}})

Two parameters are offset and number of returns

11. Query for embedded documents


12. Powerful $where query Db.foo.find ();
{"_id": ObjectId ("4e17ce0ac39f1afe0ba78ce4"), "a": 1, "B": 3, "C": 10}
{"_id": ObjectId ("4e17ce13c39f1afe0ba78ce5"), "a": 1, "B": 6, "C": 6}

What to do if you want to query for a document B = C.

> Db.foo.find ({"$where": function () {for (var.
For (Var other in this) {
if (current!= other && this[current] = = This[other]) {
return true;
}
}
}
return false;

}});

{"_id": ObjectId ("4e17ce13c39f1afe0ba78ce5"), "a": 1, "B": 6, "C": 6}

1). Greater than, less than, greater than or equal to, less than or equal to
$GT: Greater Than
$LT: Less than
$gte: greater than or equal to
$lte: Less than or equal to
Example:
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 or equal To:field <= value

If query J is greater than 3, less than 4:

Db.things.find ({j: {$lt: 3}});
Db.things.find ({j: {$gte: 4}});

You can also merge within a single statement:

Db.collection.find ({"field": {$gt: value1, $lt: value2}}); value1 < Field < value


2) is not equal to $ne

Example:

Db.things.find ({x: {$ne: 3}});


3) in and not in ($in $nin)
Grammar:
Db.collection.find ({"field": {$in: Array}});

Example:

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.