1. specify the returned key db. [documentName]. find ({condition}, {key specified}) data preparation persons. jsonvarpersons [{name: jim, age: 25, email: 75431457@qq.com, c: 89, m: 96, e: 87, country: USA, books: [JS, C, EXTJS, MONGODB]}, {name: tom, age: 25, email: 214557457@qq.com, c
1. specify the returned key db. [documentName]. find ({condition}, {key specified}) data preparation persons. json var persons = [{name: jim, age: 25, email: 75431457@qq.com, c: 89, m: 96, e: 87, country: USA, books: [JS, c, EXTJS, MONGODB]}, {name: tom, age: 25, email: 214557457@qq.com, c
1. Specify the keys returned
Db. [documentName]. find ({condition}, {key specified })
Data Preparation persons. json
Var persons = [{
Name: "jim ",
Age: 25,
Email: 75431457@qq.com ",
C: 89, m: 96, e: 87,
Country: "USA ",
Books: ["JS", "C ++", "EXTJS", "MONGODB"]
},
{
Name: "tom ",
Age: 25,
Email: 214557457@qq.com ",
C: 75, m: 66, e: 97,
Country: "USA ",
Books: ["PHP", "JAVA", "EXTJS", "C ++"]
},
{
Name: "lili ",
Age: 26,
Email: 344521457@qq.com ",
C: 75, m: 63, e: 97,
Country: "USA ",
Books: ["JS", "JAVA", "C #", "MONGODB"]
},
{
Name: "zhangsan ",
Age: 27,
Email: 2145567457@qq.com ",
C: 89, m: 86, e: 67,
Country: "China ",
Books: ["JS", "JAVA", "EXTJS", "MONGODB"]
},
{
Name: "lisi ",
Age: 26,
Email: 274521457@qq.com ",
C: 53, m: 96, e: 83,
Country: "China ",
Books: ["JS", "C #", "PHP", "MONGODB"]
},
{
Name: "wangwu ",
Age: 27,
Email: 65621457@qq.com ",
C: 45, m: 65, e: 99,
Country: "China ",
Books: ["JS", "JAVA", "C ++", "MONGODB"]
},
{
Name: "zhaoliu ",
Age: 27,
Email: 214521457@qq.com ",
C: 99, m: 96, e: 97,
Country: "China ",
Books: ["JS", "JAVA", "EXTJS", "PHP"]
},
{
Name: "piaoyingjun ",
Age: 26,
Email: piaoyingjun@uspcat.com ",
C: 39, m: 54, e: 53,
Country: "Korea ",
Books: ["JS", "C #", "EXTJS", "MONGODB"]
},
{
Name: "lizhenxian ",
Age: 27,
Email: lizhenxian@uspcat.com ",
C: 35, m: 56, e: 47,
Country: "Korea ",
Books: ["JS", "JAVA", "EXTJS", "MONGODB"]
},
{
Name: "lixiaoli ",
Age: 21,
Email: lixiaoli@uspcat.com ",
C: 36, m: 86, e: 32,
Country: "Korea ",
Books: ["JS", "JAVA", "PHP", "MONGODB"]
},
{
Name: "zhangsuying ",
Age: 22,
Email: zhangsuying@uspcat.com ",
C: 45, m: 63, e: 77,
Country: "Korea ",
Books: ["JS", "JAVA", "C #", "MONGODB"]
}]
For (var I = 0; I Db. persons. insert (persons [I])
}
1.1 query the specified key (name, age, country) of all data)
Db. persons. find ({}, {name: 1, age: 1, country: 1, _ id: 0 })
2. query Conditions
2. query Conditions
2.1 students aged between 25 and 27
Db. persons. find ({age: {$ gte: 25, $ lte: 27 },{ _ id: 0, age: 1 })
2.2 query the mathematical scores of all Korean students
Db. persons. find ({country: {$ ne: "Korea" }},{ _ id: 0, m: 1 })
3. include or not include
$ In or $ nin
2.3 query student information whose nationality is China or the United States.
Db. persons. find ({country :{$ in: ["USA", "China"]})
2.4 query student information whose nationality is not China or the United States.
Db. persons. find ({country :{$ nin: ["USA", "China"]})
4. OR query
$ Or
2.4 Query Information of students whose Chinese scores are greater than 85 or whose English scores are greater than 90
Db. persons. find ({$ or: [{c: {$ gte: 85 },{ e: {$ gte: 90}] },{ _ id: 0, c: 1, e: 1 })
5. Null
Adds a new sex key to Chinese students.
Db. person. update ({country: "China"}, {$ set: {sex: "m" }}, false, true)
2.5 query the students whose sex is null
Db. persons. find ({sex: {$ in: [null] },{ country: 1 })
6. Regular Expression Query
2.6 Query Information of students whose names contain "li ".
Db. persons. find ({name:/li/I}, {_ id: 0, name: 1 })
7. $ not usage
$ Not can be used anywhere for reverse operations
2.7 query the student information whose name does not contain "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 uses a set.
8. array query $ all and index applications
2.8 Query students who like to watch MONGOD and JS
Db. persons. find ({books: {$ all: ["MONGOBD", "JS"] },{ books: 1, _ id: 0 })
2.9 querying the second book is the learning information of JAVA
Db. persons. find ({"books.1": "JAVA "})
9. query the specified length array $ size. It cannot be used with comparison query characters (this is a drawback)
2.8. The number of favorite books is 4.
Db. persons. find ({books: {$ size: 4 }}, {_ id: 0, books: 1 })
2.9 query the number of students who prefer more than 3 books
1. Add the field size
Db. persons. update ({},{$ set: {size: 4 }}, false, true)
2. Change the update method of books. The size increases by 1 each time a book is added.
Db. persons. update ({queryer}, {$ push: {books: "ORACLE"}, $ inc: {size: 1 }})
3. query with $ gt
Db. persons. find ({size :{$ gt: 3 }})
2.10 use shell to query the number of books Jim liked
Var persons = db. persons. find ({name: "jim "})
While (persons. hasNext ()){
Obj = persons. next ();
Print (obj. books. length)
}
Lesson Summary
1. mongodb is a NOSQL database, but it is still very powerful in document query.
2. the query operator basically uses the updated character in the curly brackets to be out.
3. shell is a thorough JS engine, but some special operations depend on it
Various driver packages (JAVA, NODE. JS)
10. $ the slice operator returns the internal value of the specified array in the document
2.11 check 2nd ~ 4 books
Db. persons. find ({name: "jim" },{ books: {"$ slice": [1, 3]})
2.12 query the last book
Db. persons. find ({name: "jim"}, {books: {"$ slice":-1}, _ id: 0, name: 1 })
11. Document Query
Add the resume document jim. json for jim
2.13 query students who have studied at K
1. This can be done with absolute match, but there are some problems (looking for a problem? Order? Always carry score ?)
Db. persons. find ({school: "K", score: "A" }},{ _ id: 0, school: 1 })
2. In order to solve the sequence problem, I can use the object "." To locate the problem.
Db. persons. find ({"school. score": "A", "school. school": "K" },{ _ id: 0, school: 1 })
3. Let's look at the example:
Db. persons. find ({"school. score": "A", "school. school": "J" },{ _ id: 0, school: 1 })
We can also find the data just now, because score and school will compare it with other objects.
4. correct practice: query a single condition group $ elemMatch
Db. persons. find ({school :{$ elemMatch: {school: "K", score: ""}}})
12. $ where
12. query information about students who are older than 22 years old and who like to read C ++ books and have studied at K school.
For complex queries, we can use $ where because it is omnipotent.
However, we should try to avoid using it as little as possible because it will cost 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;
}
}
}
}
}})
Ii. Paging and sorting
1. the specified number of data entries returned by Limit
1.1 query the first five pieces of data in the persons document.
Db. persons. find ({},{ _ id: 0, name: 1}). limit (5)
2. Skip return the specified data Span
2.1 query the persons documentation 5 ~ 10 pieces of data
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: The keys of mongodb can be stored in different types of data for sorting and priority.
Minimum value
Null
Number
String
Object/Document
Array
Binary
Object ID
Boolean
Date
Maximum timestamp (à regxà)
4. Complete pagination of Limit and Skip
4.1 one page with three data bits
Page 1, à db. persons. find ({}, {_ id: 0, name: 1}). limit (3). skip (0)
Page 2, à db. persons. find ({}, {_ id: 0, name: 1}). limit (3). skip (3)
4.2skip has performance problems. We can also change our thinking without special circumstances.
Re-deconstruct the document
Each query operation saves the date of the last document in the foreground and background.
Db. persons. find ({date: {$ gt: date value}). limit (3)
Personal suggestion: You should place the midpoint of the software on convenient and precise queries instead of paging performance.
Because the user does not review up to two pages
Iii. cursor
Query data using cursors
Var persons = db. persons. find ();
While (persons. hasNext ()){
Obj = persons. next ();
Print (obj. name)
}
2. cursors and destruction conditions
1. The client sends a message asking him to destroy it.
2. cursor iteration is complete
3. The default cursor will not be cleared if it is useless for more than 10 minutes.
3. querying snapshots
After the snapshot is taken, the cursor motion will be performed for the unchanged set. Let's take a look at the usage.
Db. persons. find ({$ query: {name: "Jim"}, $ snapshot: true })
Advanced query options
$ Query
$ Orderby
$ Maxsan: Maximum number of Documents scanned by integer
$ Min: Start of doc Query
$ Max: End of doc Query
$ Hint: The index used by the doc.
$ Explain: boolean statistics
$ Snapshot: boolean consistent snapshot