Skip to content

Instantly share code, notes, and snippets.

@vijaydeepak-tt
Created November 17, 2017 17:21
Show Gist options
  • Save vijaydeepak-tt/b974015c4db36239f23823984efeb42a to your computer and use it in GitHub Desktop.
Save vijaydeepak-tt/b974015c4db36239f23823984efeb42a to your computer and use it in GitHub Desktop.
1) use dbName --- goes to selected db, if it not exist it will create a new db on that name.
2) db --- returns the name of the current db we selected
3) show dbs --- returns the list of dbs which contains any datas.
4) db.collectionName.insert(json object) --- inserts a data inside the selected collection name.
5) db.dropDatabase() --- drops the current database.
6) db.createCollection("collectionName") --- creates new collection in a selected db.
7) show collections --- returns all exist collections.
8) db.collectionName.drop() --- drops the collection.
9) db.collectionName.find() --- returns all json objects in the collection.
10) db.collectionName.find({}, {"key": 1}) --- returns only the selected key and its value of all objects inside the collection.
11) db.collectionName.find({}, {"key": 0}) --- returns all the key value pairs except the selected key and its value of all the objects in the collection.
12) db.collectionName.find({}, {"key": 1, "key": 0}) --- multi functions.
13) db.collectionName.find({}, {"key": 1, "key2": 1, "key3": 0}).limit(3) --- returns the top 3(as mentioned in the limit parameter).
14) db.collectionName.find({}, {"key": 1, "key2": 1, "key3": 0}).skip(3) --- skips the top 3(as mentioned in the skip parameter).
15) db.collectionName.find({}, {"key": 1, "key2": 1, "key3": 0}).skip(3).limit(2)
16) db.collectionName.find({}, {"key": 1, "key2": 1, "key3": 0}).sort({"key": 1}) --- sorts the objects based on the selected key's values in assending(1) or dessending(-1).
17) db.collectionName.findOne({"key": value}) --- returns particular object.
18) db.collectionName.ensureIndex({"key": 1}) --- creates the index for our selected collection to make search data in large collection.
19) db.collectionName.dropIndex({"key": 1}) --- removes the index for the collection.
20) db.collectionName.aggregate([{$group: {_id: "$key", Result: {$sum: 1} } }]) --- sums the data based on the key and returns the each keys count.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment