Created
February 5, 2019 06:42
-
-
Save colxi/ea56d6cfbd68c1f603be19f85f6bad86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Connect to MongoDb shell | |
--------------------------- | |
mongo "<server-address>/<database>" --username <username> --password <password> | |
MongoDb shell commands | |
--------------------------- | |
Show selected db | |
> db | |
List databases | |
> show dbs | |
Select database | |
> use <dbName> | |
List active databaae collectuons | |
> show collections | |
CRUD operatins | |
db.<collection>.[find|insert|update|replace|delete](<selector>) | |
Execute js file (useful for importing data) | |
> run("<path-to-js>") | |
Selectors Examples | |
-------------------------- | |
db.<collectionName>.find( {filter} ) list all documents (rows) in collectionName | |
db.<collectionName>.find( { color:"red", length: 100 } ) | |
db.<collectionName>.find( {filter} , {projection}) Set propertes to be returned (prop:boolean) | |
db.<collectionName>.find( { color:"red", length: 100 }, { color: true } ) | |
db.<collectionName>.find( {"tripduration": {$exists: true} } ) <---return only documnts that have te specified property defined | |
Nested ObJects (dot notation) | |
---------------------------------- | |
db.<collectionName>.find( { "box.color":"red"} ) <--- quotes are needed for nested objects | |
Arrays Manipulation | |
---------------------------------- | |
db.<collectionName>.find( { "actors": ["Arnold"]} ) <--- get docs where single index array has Arnold | |
db.<collectionName>.find( { "actors": ["Arnold","Lina"]} ) <--- get docs where actors matches (strict) | |
db.<collectionName>.find( { "actors":"Arnold"} ) <--- get entries where actors contains Arnold | |
db.<collectionName>.find( { "actors.0":"Arnold"} ) <--- get entries where actors index 0 is Arnold | |
db.<collectionName>.find( {countries: {$size:2}} ) // eturn all documents where the countrynarray has only 2 elements | |
db.<collectionName>.find( {$or : [ {watlev:"always dry"}, {depth: 0}] } ) // watlev=="always dry" || depth==0 | |
db.<collectionName>.find( {JSON_filters} ).count() | |
db.<collectionName>.find( {JSON_filters} ).pretty() | |
db.<collectionName>.insertOne({JSON}) Inserts the document defined using JSON | |
db.<collectionName>.insertMany([ {JSON} ,{JSON} ,...]) Inserts the documents defined using JSON Array | |
db.<collectionName>.insertMany([ {JSON} ,{JSON} ,...] , {ordered:true|false} ) | |
- ordered insertn(default) : stops inserting after an error has been found | |
- unordered insert : keeps going even in¡f errors are encontuntered | |
db.<collectionName>.updateOne({JSON}, { $set:{ <field>:value } } ) Updates the first document that matcjes the selector | |
db.<collectionName>.updateMany({JSON}, { $set:{ <field>:value } } ) Updates all documents that match the selector | |
db.<collectionName>.updateMany({JSON}, { $set:{ <field>:value } }, {upsert:true} ) if the document selected by the query does not exist create it | |
db.<collectionName>.updateOne({JSON}, { $inc:{ <field>: incrementValue } } ) Increments | |
db.<collectionName>.updateOne({JSON}, { $push: { <field>: value } } ) Push to array | |
db.<collectionName>.updateOne( { _id: 1 }, { $push: { scores: 89 } } ) | |
db.<collectionName>.updateOne( { _id: 1 }, { $push: { scores: { $each: [ 90, 92, 85 ] } } } ) | |
db.<collectionName>.updateOne( { _id: 1 }, { $unset: { <field> : false } } ) delete a field | |
db.<collectionName>.replaceOne( { _id: 1 }, { document } ) replaces the document of the query | |
db.<collectionName>.deleteOne( { _id: 1 } ) | |
db.<collectionName>.deleteMany( { selector } ) | |
Compass: | |
filter | |
{genres: "Comedy"} // returns all documents wich genere contains Comedy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment