MongoDB Learning notes < four >

Source: Internet
Author: User

Today continue to learn about MongoDB's knowledge, mainly including the following for example:

--find Specific explanations

--Pagination and sorting

-Cursors and other knowledge


1. Specify the key to return

Db.person.find ({},{"_id": 0, "name": 1, "Country": 1}) This simply queries the name key and the country key, noting that the other assumptions do not appear by default, but assume that the _id hypothesis is not written and is displayed by default.

2. Query criteria

Db.person.find ({"Age": {$gte: +, $lte: 27}},{"_id": 0, "name": 1, "Country": 1}) Check the name and country of the student aged 25-27.

Db.person.find ({"Country": {$ne: "Korea"}},{"_id": 0, "math": 1}) query all not the math scores of Korean students

3. Include or not include

$in or $nin

Db.person.find ({"Country": {$in: ["USA", "China"]}},{"_id": 0, "math": 1}) Query nationality is an American or Chinese student's math score

Db.person.find ({"Country": {$nin: ["USA", "China"]}},{"_id": 0, "math": 1}) Query nationality is not an American or Chinese student's math score

4.or Query

$or

Db.person.find ({$or: [{"Chinese": {$gt: 85}},{"中文版": {$gt: 90}}]},{"_id": 0, "name": 1, "Chinese": 1, "中文版": 1}) Query for student information with a language score greater than 85 or English bar with a score greater than 90

5. Regular queries

Db.person.find ({name:/li/i},{"_id": 0, "name": 1}) The name of the student whose name contains "Li"

6. Use of $not

Db.person.find ({name:{$not:/li/i}},{"_id": 0, "name": 1}) name of the student whose name does not contain "Li"

Note: The difference between $not and $nin is that $not can be used wherever, and $nin can only be used on the set

7. Array query $all and index applications

Query the students who like to look at the two books MongoDB and JS

Db.person.find ({books:{$all: ["MongoDB", "JS"]}},{"_id": 0, "name": 1})

Query the second book is the student information of Java

Db.person.find ({"Books.1": "JAVA"})

8. Querying the specified length array size

Db.person.find ({"Books": {$size: 4}})

9. Find out the number of Jim's books

var Jim =  db.person.find ("name": "Jim") while (Jim.hasnext ()) {   obj = Jim.next ();   Print (Obj.books.length)}
The $slice operator returns the internal value of the specified array in the document.

Find out the 第2-4 book in Jim's Bookshelf

Db.person.find ({"Name": "Jim"},{books:{$slice: [1,3]}})

Check out the last book

Db.person.find ({"Name": "Jim"},{books:{$slice:-1}})


This time first, go ahead, cursor and sort.







MongoDB Learning notes < four >

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.