Continue to learn about MongoDB today, mainly including the following:
--find detailed
--Pagination and sorting
-Cursors and other knowledge
1. Specify the key to return
Db.person.find ({},{"_id": 0, "name": 1, "Country": 1}) This is only the name key and the country key query out, note the other if not write the default does not display, but if the _id if not written, the default is displayed.
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 the math scores that are not Korean students
3. Contains or does not contain
$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 anywhere, and $nin can only be used on the collection
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 >