MongoDB performs a query operation through the Find () function
1. Find all the documents in the collection:
Db.media.find ()
System returns:
{"_id": ObjectId ("5aa47c1953350e04ddbd6fa3"), "Type": "Book", "Title": "Definitive Guide to MongoDB 3rd ed.", "ISBN": "978-1-4842-1183-0", "Publisher": "Apress", "Author": ["Hows,david", "Plugge,eelco", "Membrey,peter", "Hawkins,tim"] }
{"_id": ObjectId ("5aa47dd553350e04ddbd6fa4"), "Type": "CD", "Artist": "Nirvana", "Title": "Nevermind", "tracklist": [{' track ': ' 1 ', ' titie ': ' Smells like Teen Spirit ', ' Length ': ' 5:02 '}, {' track ': ' 2 ', ' Title ': ' In Bloom ', ' lengt H ":" 4:15 "}]}
Shows the 2 documents we added in the previous article
2. Find all CDs for Nirvana:
Db.media.find ({Artist: "Nirvana"})
System returns:
{"_id": ObjectId ("5aa47dd553350e04ddbd6fa4"), "Type": "CD", "Artist": "Nirvana", "Title": "Nevermind", "tracklist": [{' track ': ' 1 ', ' titie ': ' Smells like Teen Spirit ', ' Length ': ' 5:02 '}, {' track ': ' 2 ', ' Title ': ' In Bloom ', ' lengt H ":" 4:15 "}]}
3. Find the title of all CDs for Nirvana:
Db.media.find ({Artist: "Nirvana"},{title:1})
System returns:
{"_id": ObjectId ("5aa47dd553350e04ddbd6fa4"), "Title": "Nevermind"}
Find non-header information for all CDs of Nirvana
Db.media.find ({Artist: "Nirvana"},{title:0})
System returns:
{"_id": ObjectId ("5aa47dd553350e04ddbd6fa4"), "Type": "CD", "Artist": "Nirvana", "tracklist": [{"Track": "1", "tit ie ":" Smells like Teen Spirit "," Length ":" 5:02 "}, {' track ':" 2 "," Title ":" In Bloom "," Length ":" 4:15 "}]}
4. Find information about the inline object document:
Db.media.find ({"Tracklist.title": "In Bloom"})
The system returns all the information for the corresponding document
{"_id": ObjectId ("5aa47dd553350e04ddbd6fa4"), "Type": "CD", "Artist": "Nirvana", "Title": "Nevermind", "tracklist": [{' track ': ' 1 ', ' titie ': ' Smells like Teen Spirit ', ' Length ': ' 5:02 '}, {' track ': ' 2 ', ' Title ': ' In Bloom ', ' lengt H ":" 4:15 "}]}
5 Other related functions
Sort () sorting function db.media.find (). Sort ({title:1}) #1为升序, 1 is descending
Limit () limits the maximum number of returned results db.media.find (). Limit (Ten) #0表示返回所有结果
Skip () ignores the first few documents in the collection Db.media.find (). Skip (20)
Simple use of MongoDB-query operations