Difference between revisions of "User:Saul/Mongodb"
From Organic Design wiki
(Added some useful MongoDB commands) |
m (Fixed indentation issue.) |
||
Line 1: | Line 1: | ||
== Useful Mongodb commands: == | == Useful Mongodb commands: == | ||
<source lang="bash"> | <source lang="bash"> | ||
− | + | show dbs # Shows all databases | |
− | + | use DBNAME # Select the database DBNAME | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | show collections # show collections in the current database - collections are a bit like MySQL tables | |
− | + | db.createCollection("CNAME") # creates a collection in the current database | |
− | + | db.CNAME # show documents under collection CNAME - documents are a bit like MySQL rows | |
− | + | db.CNAME.find({}) # return all objects under the collection CNAME | |
− | + | db.CNAME.find({KEY : VALUE}) # return all objects matching the KEY : VALUE pair under the collection CNAME | |
− | + | db.CNAME.find({}).pretty() # return all objects under the collection CNAME in a nicer/readable format | |
− | + | db.CNAME.find({}).sort({units: 1}) # return in ascending order - change to -1 for descending | |
− | + | db.CNAME.find({}).sort({name: 1}) # return in descending alphabetical order - change to -1 for ascending | |
− | + | db.CNAME.find({}).limit(10) # return only the first 10 results | |
− | + | ||
+ | db.CNAME.insert(JSON) # inserts the JSON object into the collection CNAME | ||
+ | db.CNAME.update({ {KEY : VALUE}, {$set: JSON} }) # updates the document found under the collection CNAME with the matching KEY : VALUE pair with the new data - JSON | ||
+ | db.CNAME.update({ {KEY : VALUE}, {$set: JSON} }, {$upsert: true}) # adds new fields in the JSON | ||
+ | |||
+ | db.CNAME.remove({}) # remove all documents from the collection CNAME | ||
+ | db.CNAME.remove({KEY : VALUE}) # remove document that has a matching KEY : VALUE pair from the collection CNAME | ||
+ | |||
+ | {KEY : {$gt : 10}} # returns all items that have KEY value greater than 10 | ||
+ | {KEY : {$lt : 10}} # returns all items that have KEY value less than 10 | ||
+ | {KEY : { $in : ["VALUE1", "VALUE2"] }} # returns all items that have KEY equal to "VALUE1" and "VALUE2" - VALUE being of type array | ||
+ | {KEY : { $or : [ {KEY: VALUE1}, {KEY: VALUE2} ] }} # returns all items that have KEY equal to VALUE1 or VALUE2 | ||
</source> | </source> |
Revision as of 03:15, 14 March 2018
Useful Mongodb commands:
show dbs # Shows all databases
use DBNAME # Select the database DBNAME
show collections # show collections in the current database - collections are a bit like MySQL tables
db.createCollection("CNAME") # creates a collection in the current database
db.CNAME # show documents under collection CNAME - documents are a bit like MySQL rows
db.CNAME.find({}) # return all objects under the collection CNAME
db.CNAME.find({KEY : VALUE}) # return all objects matching the KEY : VALUE pair under the collection CNAME
db.CNAME.find({}).pretty() # return all objects under the collection CNAME in a nicer/readable format
db.CNAME.find({}).sort({units: 1}) # return in ascending order - change to -1 for descending
db.CNAME.find({}).sort({name: 1}) # return in descending alphabetical order - change to -1 for ascending
db.CNAME.find({}).limit(10) # return only the first 10 results
db.CNAME.insert(JSON) # inserts the JSON object into the collection CNAME
db.CNAME.update({ {KEY : VALUE}, {$set: JSON} }) # updates the document found under the collection CNAME with the matching KEY : VALUE pair with the new data - JSON
db.CNAME.update({ {KEY : VALUE}, {$set: JSON} }, {$upsert: true}) # adds new fields in the JSON
db.CNAME.remove({}) # remove all documents from the collection CNAME
db.CNAME.remove({KEY : VALUE}) # remove document that has a matching KEY : VALUE pair from the collection CNAME
{KEY : {$gt : 10}} # returns all items that have KEY value greater than 10
{KEY : {$lt : 10}} # returns all items that have KEY value less than 10
{KEY : { $in : ["VALUE1", "VALUE2"] }} # returns all items that have KEY equal to "VALUE1" and "VALUE2" - VALUE being of type array
{KEY : { $or : [ {KEY: VALUE1}, {KEY: VALUE2} ] }} # returns all items that have KEY equal to VALUE1 or VALUE2