First add a few documents:
DVD = ({"Type": "DVD", "Title": "Matrix,the", "released": 1994, "Cast": ["Keanu Reeves", "Carrie-anne Moss", "Laurence Fishburne "," Hugo Weaving "," Gloria Foster "," Joe Pantoliano "]})
Db.media.insertOne (DVD)
DVD = ({"Type": "DVD", "Title": "Blade Runner", "released": 1982})
Db.media.insertOne (DVD)
DVD = ({"Type": "DVD", "Title": "Toy Story 3", "Released": 2010})
Db.media.insertOne (DVD)
1. Compare symbols ($GT, $gte, $lt, $lte) # (greater than, greater than, equal to, less than, less than or equal to)
Db.media.find ({released:{$gt: 2000}},{"Cast": 0})
System returns
{"_id": ObjectId ("5aa4984153350e04ddbd6fab"), "Type": "DVD", "Title": "Toy Story 3", "Released": 2010}
Db.media.find ({released:{$gte: 1994, $lt: 2010}},{"Cast": 0})
System returns
{"_id": ObjectId ("5aa497c753350e04ddbd6fa9"), "Type": "DVD", "Title": "Matrix,the", "released": 1994}
2. $in, $all, $or
Db.media.find ({released:{$in: [1994,2008,2009]}},{"Cast": 0}
System returns
{"_id": ObjectId ("5aa497c753350e04ddbd6fa9"), "Type": "DVD", "Title": "Matrix,the", "released": 1994}
Db.media.find ({released:{$all: [1994,2008,2009]}},{"Cast": 0})
System returns
(There is no document that satisfies 3 conditions at the same time, returns null)
Db.media.find ({$or: [{"Title": "Toy Story 3"},{"ISBN": "978-1-4842-1183-0"}]})
System returns
{"_id": ObjectId ("5aa490f053350e04ddbd6fa6"), "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"] }
3. $slice
Db.media.find ({"Title": "Matrix,the"},{"Cast": {$slice: 3}})
System returns
{"_id": ObjectId ("5aa497c753350e04ddbd6fa9"), "Type": "DVD", "Title": "Matrix,the", "released": 1994, "Cast": ["Kea Nu Reeves "," Carrie-anne Moss "," Laurence Fishburne "]}
Db.media.find ({"Title": "Matrix,the"},{"Cast": {$slice: [ -5,4]}}) #从最后5项中截取前4个
System returns
{"_id": ObjectId ("5aa497c753350e04ddbd6fa9"), "Type": "DVD", "Title": "Matrix,the", "released": 1994, "Cast": ["Car Rie-anne Moss "," Laurence Fishburne "," Hugo Weaving "," Gloria Foster "]}
4. $mod
Db.media.find ({released:{$mod: [3,2]}},{"Cast": 0})
System returns:
{"_id": ObjectId ("5aa497c753350e04ddbd6fa9"), "Type": "DVD", "Title": "Matrix,the", "released": 1994}
{"_id": ObjectId ("5aa4981653350e04ddbd6faa"), "Type": "DVD", "Title": "Blade Runner", "released": 1982}
5. $size
Db.media.find ({tracklist:{$size: 2}})
System returns:
{"_id": ObjectId ("5aa4912953350e04ddbd6fa7"), "Type": "CD", "Artist": "Nirvana", "Title": "Nevermind", "tracklist": [{' track ': ' 1 ', ' title ': ' Smells like Teen Spirit ', ' Length ': ' 5:02 '}, {' track ': ' 2 ', ' title ': ' In Bloom ', ' lengt H ":" 4:15 "}]}
6. $exists
Db.media.find ({author:{$exists: true}})
System returns:
{"_id": ObjectId ("5aa490f053350e04ddbd6fa6"), "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 ("5aa4915453350e04ddbd6fa8"), "Type": "Book", "Title": "Definitive Guide to MongoDB 3rd ed.", "ISBN": "978-1-4842-1183-1", "Publisher": "Apress", "Author": ["Hows,david", "Plugge,eelco", "Membrey,peter", "Hawkins,tim"] }
Db.media.find ({author:{$exists: false}})
System returns:
{"_id": ObjectId ("5aa4912953350e04ddbd6fa7"), "Type": "CD", "Artist": "Nirvana", "Title": "Nevermind", "tracklist": [{' track ': ' 1 ', ' title ': ' Smells like Teen Spirit ', ' Length ': ' 5:02 '}, {' track ': ' 2 ', ' title ': ' In Bloom ', ' lengt H ":" 4:15 "}]}
{"_id": ObjectId ("5aa497c753350e04ddbd6fa9"), "Type": "DVD", "Title": "Matrix,the", "released": 1994, "Cast": ["Kea Nu Reeves "," Carrie-anne Moss "," Laurence Fishburne "," Hugo Weaving "," Gloria Foster "," Joe Pantoliano "]}
{"_id": ObjectId ("5aa4981653350e04ddbd6faa"), "Type": "DVD", "Title": "Blade Runner", "released": 1982}
{"_id": ObjectId ("5aa4984153350e04ddbd6fab"), "Type": "DVD", "Title": "Toy Story 3", "Released": 2010}
7. $type
Db.media.find ({tracklist:{$type: 3}}) #type: 3 for embedded objects
{"_id": ObjectId ("5aa4912953350e04ddbd6fa7"), "Type": "CD", "Artist": "Nirvana", "Title": "Nevermind", "tracklist": [{' track ': ' 1 ', ' title ': ' Smells like Teen Spirit ', ' Length ': ' 5:02 '}, {' track ': ' 2 ', ' title ': ' In Bloom ', ' lengt H ":" 4:15 "}]}
8. $elemMatch
Db.media.find ({"Tracklist.title": "Smells like Teen Spirit", "Tracklist.track": "1"})
System returns:
{"_id": ObjectId ("5aa4912953350e04ddbd6fa7"), "Type": "CD", "Artist": "Nirvana", "Title": "Nevermind", "tracklist": [{' track ': ' 1 ', ' title ': ' Smells like Teen Spirit ', ' Length ': ' 5:02 '}, {' track ': ' 2 ', ' title ': ' In Bloom ', ' lengt H ":" 4:15 "}]}
{"_id": ObjectId ("5aa49fe653350e04ddbd6fac"), "Type": "CD", "Artist": "Nirvana", "Title": "Nirvana", "tracklist": [ {' track ': ' 1 ', ' title ': ' You Know ' is right ', ' Length ': ' 3:38 '}, {' track ': ' 5 ', ' title ': ' Smells like Teen Spi Rit "," Length ":" 5:02 "}]}
Db.media.find ({tracklist:{$elemMatch: {Title: ' Smells like Teen Spirit ', Track: ' 1 '}}})
System returns:
{"_id": ObjectId ("5aa4912953350e04ddbd6fa7"), "Type": "CD", "Artist": "Nirvana", "Title": "Nevermind", "tracklist": [{' track ': ' 1 ', ' title ': ' Smells like Teen Spirit ', ' Length ': ' 5:02 '}, {' track ': ' 2 ', ' title ': ' In Bloom ', ' lengt H ":" 4:15 "}]}
9. Regular expressions
Db.media.find ({title:/^matrix*/i}) #i表示不区分大小写
System returns:
{"_id": ObjectId ("5aa497c753350e04ddbd6fa9"), "Type": "DVD", "Title": "Matrix,the", "released": 1994, "Cast": ["Kea Nu Reeves "," Carrie-anne Moss "," Laurence Fishburne "," Hugo Weaving "," Gloria Foster "," Joe Pantoliano "]}
MongoDB simple to use-query operation 3