Mongodb Conditional Query

Source: Internet
Author: User

1.1 The specified key for all data is queried (name, age, country)

Db.persons.find ({},{name:1,age:1,country:1,_id:0})

2. Query criteria

2. Query criteria

2.1 Check out students aged between 25-27 years

Db.persons.find ({age: {$gte: $lte: 27},{_id:0,age:1})

2.2 Find out all the math scores of students who are not Korean

Db.persons.find ({country:{$ne: "Korea"}},{_id:0,m:1})

3. Contains or does not contain

$in or $nin

2.3 Enquiries about nationality is Chinese or American student information

Db.persons.find ({country:{$in: ["USA", "China"]})

2.4 Enquiries about nationality is not a student information in China or USA

Db.persons.find ({country:{$nin: ["USA", "China"]})

4.OR Query

$or

2.4 Enquiries about students with language scores greater than 85 or English greater than 90

Db.persons.find ({$or: [{c:{$gte: 85}},{e:{$gte: 90}}]},{_id:0,c:1,e:1})

5.Null

Add new key sex to Chinese students

Db.person.update ({country: "China"},{$set: {sex: "M"}},false,true)

2.5 query for a student with sex equal to null

Db.persons.find ({sex:{$in: [Null]}},{country:1})

6. Regular queries

2.6 Find information about students with "Li" in their names

Db.persons.find ({name:/li/i},{_id:0,name:1})

7. Use of $not

$not can be used anywhere to reverse the operation

2.7 Find information about students whose name does not exist in "Li"

Db.persons.find ({name:{$not:/li/i}},{_id:0,name:1})

The difference between $not and $nin is that $not can be used anywhere, $nin is used on a set.

8. Array query $all and index applications

2.8 Queries students who like to see Mongod and JS

Db.persons.find ({books:{$all: ["MONGOBD", "JS"]}},{books:1,_id:0})

2.9 Query The second book is the Learning information of Java

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

9. Querying a specified length array $size it cannot be used with a comparison query (this is a disadvantage)

2.8 The number of books I like is 4 students

Db.persons.find ({books:{$size: 4}},{_id:0,books:1})

2.9 Students who like more than 3 books

1. Increase the field size

Db.persons.update ({},{$set: {size:4}},false,true)

2. Change the way books are updated, size increases by 1 each time you add a book

Db.persons.update ({},{$push: {books: "ORACLE"}, $inc: {size:1}})

3. Using $GT Query

Db.persons.find ({size:{$gt: 3}})

2.10 Using the shell to find out the number of books Jim likes to read

var persons = Db.persons.find ({name: "Jim"})

while (Persons.hasnext ()) {

obj = Persons.next ();

Print (Obj.books.length)

}

Summary of Recess

1.mongodb is a NoSQL database, but he's still very powerful in document queries.

2. The query is basically used in curly braces inside the update character is basically outside

3.shell is a thoroughly-bottomed JS engine, but some special operations depend on his

Each driver package to complete (java,node. JS)

The $slice operator returns the internal value of the specified array in the document

2.11 Find out the 2nd to 4th book in Jim's bookshelf.

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

2.12 Check out the last book

Db.persons.find ({name: "Jim"},{books:{"$slice": -1},_id:0,name:1})

11. Document Query

Add a curriculum vitae document to Jim Jim.json

2.13 Check out students who have studied at K

1. We can do this with an absolute match, but there are some problems (looking for a problem?). Always take score with you?)

Db.persons.find ({school:{school: "K", Score: "A"}},{_id:0,school:1})

2. In order to solve the problem of order I can use the object "." The way to locate

Db.persons.find ({"School.score": "A", "School.school": "K"},{_id:0,school:1})

3. So also the question looks example:

Db.persons.find ({"School.score": "A", "School.school": "J"},{_id:0,school:1})

The same data can be found, because score and school are going to compare to other objects.

4. Correct practice single condition group query $elemmatch

Db.persons.find ({school:{$elemMatch: {school: "K", Score: "A"}})

$where.

12. Inquiries older than 22 years old, like to read C + + books, students in the K school to learn the information

Complex queries we can use $where because he's omnipotent.

But we have to avoid using it as much as possible because he will have the cost of performance.

Db.persons.find ({"$where": function () {

var books = This.books;

var school = This.school;

if (This.age > 22) {
var php = null;

for (var i = 0; i < books.length; i++) {
if (books[i] = = "C + +") {
php = Books[i];

if (school) {
for (var j = 0; J < School.length; J + +) {

if (School[j].school = = "K") {

return true;
}
}
Break
}
}
}
}
}})

Second, pagination and sorting

1.Limit returns the specified number of data bars

1.1 Query out the first 5 data in persons document

Db.persons.find ({},{_id:0,name:1}). Limit (5)

2.Skip returns the span of the specified data

2.1 Querying the data from the 5~10 bar in the persons document

Db.persons.find ({},{_id:0,name:1}). Limit (5). Skip (5)

3.Sort returns data sorted by age [1,-1]

Db.persons.find ({},{_id:0,name:1,age:1}). Sort ({age:1})

Note: MongoDB keys can be sorted with different types of data and also have priority

Minimum value

Null

Digital

String

Objects/Documents

Array

Binary

Object ID

Boolean

Date

Time stamp à regular à maximum value

4.Limit and Skip completing pagination

4.1 Three data bits one page for paging

First page Àdb.persons.find ({},{_id:0,name:1}). Limit (3). Skip (0)

Second page àdb.persons.find ({},{_id:0,name:1}). Limit (3). Skip (3)

4.2skip has a performance problem, no special circumstances we can change the idea

Re-deconstruct the document design

Each time the query operation of the back and forth values to the last document to save the date

Db.persons.find ({date:{$gt: Date Value}}). Limit (3)

Personal suggestion à should put the midpoint of the software on a convenient and accurate query instead of paging performance

Because the user will not be able to search for up to 2 pages

Third, cursors

Using cursors to traverse query data

var persons = Db.persons.find ();

while (Persons.hasnext ()) {

obj = Persons.next ();

Print (Obj.name)

}

2. Cursor several destruction conditions

1. The client sends him a message to destroy it.

2. Cursor Iteration Complete

3. The default cursor is more than 10 minutes and will not be cleared.

3. Querying snapshots

After the snapshot, the cursor movement is made for the invariant collection, and the method of use is seen.

Db.persons.find ({$query: {name: "Jim"}, $snapshot: true})

Advanced query Options

$query

$orderby

$maxsan: The maximum number of documents scanned by integer

$min: Doc Query starts

$max: Doc Query ends

$hint: Which index is used by doc

$explain: Boolean statistics

$snapshot: Boolean Consistent snapshot

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.