Skip to content

Instantly share code, notes, and snippets.

@georgeyord
Last active August 29, 2015 14:00
Show Gist options
  • Save georgeyord/11374479 to your computer and use it in GitHub Desktop.
Save georgeyord/11374479 to your computer and use it in GitHub Desktop.
MongoDB newbie findings

Schema changes

Show all databases

show dbs;

Use a specific database

use testdb;

Create a new database by using the same command

use testdb;

Show all database collections

show collections

Create a new collection by inserting a document

db.testData.insert({example: 1}}

Search

Find documents without any conditions

db.mycollection.find()

Find documents where country is 'UK'

db.mycollection.find({country: "UK"})

Find documents with if a field missing

db.mycollection.find( { "price" : { "$exists" : false } } ) ref: http://docs.mongodb.org/manual/reference/operator/query/exists/#op._S_exists

Insert/Update

Create a new document

db.testData.insert({example: 1}}

Update an existing document

db.testData.update({example: 1},{ $set: {example: 1}}

Donk specific

Get how many prices and how much budget is per sku

db.price.aggregate([{$group: {_id: "$_id.sku_id",count: { $sum: 1 },total: {$sum: "$price.amount"}}}])

References

http://docs.mongodb.org/manual/tutorial/getting-started/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment