Percona Live 2017 Santa Clara, California April 24th 27th, 2017
MongoDB Shell: A Primer Rick Golba
The Mongo Shell It is a JavaScript interface to MongoDB Part of the standard installation of MongoDB Used to query and update data, and perform administrative operations Stored by default in /usr/bin Either access the shell from /usr/bin or add the path to the PATH variable $ PATH=$PATH:/usr/bin 3
Common Mongo Shell Options --port <port> - allows you to specify the port where mongod or mongos is listening; 27017 by default --host <hostname> - allows you to specify the host where mongod or mongos is running, needed when accessing a remote host --username <username> - allows you to specify the username to authenticate if the MongoDB database uses authentication --password <password> - allows you to specify the password for the provided username. If omitted, the shell prompts for the password. --help - provides information on all of the shell options for MongoDB 4
Mongo Limit and Skip You can limit the number of documents returned by a query > db.<collection>.find().limit(number) For queries that would return a large number of documents, but only a sample set is needed You can also skip a specified number of documents returned by a query > db.<collection>.find().limit(number).skip(number) If a value for limit is not specified, all documents will be initially returned, and then the specified number of documents are skipped 5
Mongo Sorting MongoDB queries do not returned data in a sorted order unless specified >db.<collection>.find().sort({<key1>:1,<key2>:1...}) 1 specifies an ascending sort, which is the default -1 species a descending sort Be careful of multiple sort levels slowing down the query process Queries can be sped up through proper indexing 6
Mongo Aggregation Pipeline db.orders.aggregate([ {$match: {}, {$group: {_id: "$cust_id":, total: {$sum: "$amount"}}} ] ) {cust_id: "808" amount: 724 amount: 978 amount: 603 amount: 617 status: "inactive"} 7
Mongo Aggregation Pipeline - $match db.orders.aggregate([ {$match: {}, {$group: {_id: "$cust_id":, total: {$sum: "$amount"}}} ] ) {cust_id: "808" amount: 724 amount: 978 amount: 603 amount: 617 status: "inactive"} 8
Mongo Aggregation Pipeline - $group db.orders.aggregate([ {$match: {}, {$group: {_id: "$cust_id":, total: {$sum: "$amount"}}} ] ) {cust_id: "808" amount: 724 amount: 978 amount: 603 amount: 617 status: "inactive"} {_id: "412" amount: 1702} {_id: "808" amount: 603} 9
Mongo Explain Plan db.<collection>.explain().<method> Returns information on the query plan for aggregate() count() distinct() find() group() remove() update() 10
Mongo Explain Plan Inclusions > db.orders.explain().aggregate([ {$match: {status:"active"}}, {$group: {_id:"$ cust_id",total: {$sum:"$amount"}}}]) Passes information through a series of stages to completion COLLSCAN for collection scan (all documents) IXSCAN for scan of index keys (subset of documents) FETCH for retrieval of data SHARD_MERGE for merging results from multiple shards Shows information on rejected plans, if available 11
MongoDB Write Concern By default, MongoDB only requires write acknowledgement from the master You can override this and specify the number of replicas that must acknowledge prior to the write being processed to the database This can either be a specified number of nodes or a majority of nodes To specify write concern 2 on an insert > db.movies.insert( ) { title: "Logan", year : 2017, director: "James Mangold" }, { writeconcern: { w: 2 } } Use {writeconcern: {w: "majority"}} for majority option 12
Write Concern Examples Write Concern 1 Write Concern 2 (or majority) Default Write Concern 13
validate Command Checks the structures within a namespace for correctness by scanning the collection s data and indexes Returns information regarding the on-disk representation of the collection Can be slow, particularly on larger data sets While running, it holds an exclusive lock on the collection This blocks all reads and writes until the validate command finishes Output differs based on storage engine > db.collection.validate(<true>) true specifies a complete validation and will require more time 14
validate Output Summary validate.ns - full namespace name of the collection validate.nrecords - number of documents in the collection validate.nindexes - number of indexes on the collection validate.keysperindex - number of keys in each index validate.valid - returns true if all aspects of the collection are valid validate.errors - if valid returns false, this field contains a message describing the validation error validate.ok - displays 1 for successful completion of command, 0 if it fails 15
Mongo Read Preference By default, an application sends its read requests to the primary member of a replica set You can direct the read request to another member of the replica set This may return stale data due to write concerns and asynchronous replication Setting read preference mode is specific to the driver being used 16
Mongo Read Preference Options primary - default, operations read from the current replica set primary primarypreferred - operations read from the primary but if it is unavailable, operations can read from secondary members secondary - operations read from the secondary members of the replica set secondarypreferred - operations read from secondary members but if no secondary members are available, operations read from the primary nearest - operations read from member of the replica set with the least network latency regardless of type maxstalenessseconds can be specified for all but primary to determine a time threshold after which the member is not used for reads. Must be >90. 17