SHARE
TWEET

Untitled

a guest Oct 13th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #### Show All Created Databases
  2.  
  3. ```javascript
  4. show dbs
  5. ```
  6.  
  7. #### Starte using the database named `dbname`
  8.  
  9. ```javascript
  10.  use dbname
  11. ```
  12.  
  13. #### Show a collections inside a database
  14.  
  15. ```javascript
  16. show collections
  17. ```
  18.  
  19. #### Delete/Drop a database
  20.  
  21. ```javascript
  22. db.dropDatabase();
  23. ```
  24.  
  25. #### Create database if not exist and start using it
  26.  
  27. ```javascript
  28. use dbname
  29. ```
  30.  
  31. #### Get the current databse
  32.  
  33. ```javascript
  34. db;
  35. ```
  36.  
  37. #### Create a collection named `posts`
  38.  
  39. ```javascript
  40. db.createCollection("posts");
  41. ```
  42.  
  43. #### Insert a post into `posts` collection
  44.  
  45. ```javascript
  46. db.posts.insert({ name: "Shehan", age: 24 });
  47. ```
  48.  
  49. #### Insert many posts into `posts` collection
  50.  
  51. ```javascript
  52. db.posts.insertMany([{}, {}, {}]);
  53. ```
  54.  
  55. #### Get all the posts
  56.  
  57. ```javascript
  58. db.posts.find();
  59. ```
  60.  
  61. #### Show all posts in a pretty way
  62.  
  63. ```javascript
  64. db.posts.find().pretty();
  65. ```
  66.  
  67. #### Sort all posts based on title to Ascending order
  68.  
  69. ```javascript
  70. db.posts.sort({ title: 1 }).pretty();
  71. ```
  72.  
  73. #### Count the number os posts
  74.  
  75. ```javascript
  76. db.posts.find({ category: "news" }).count();
  77. ```
  78.  
  79. #### Get only 2 posts (Limit the number of posts)
  80.  
  81. ```javascript
  82. db.posts
  83.   .find()
  84.   .limit(2)
  85.   .pretty();
  86. ```
  87.  
  88. ##### Get only 2 posts from the sorted posts
  89.  
  90. ```javascript
  91. db.posts
  92.   .find()
  93.   .sort({ title: -1 })
  94.   .limit(2)
  95.   .pretty();
  96. ```
  97.  
  98. #### Print all the names of posts
  99.  
  100. ```javascript
  101. db.posts.find().forEach(function(post) {
  102.   print("Post" + post.name);
  103. });
  104. ```
  105.  
  106. #### Get only one post from 'news' category
  107.  
  108. ```javascript
  109. db.posts.findOne({ category: "news" });
  110. ```
  111.  
  112. #### Update a post, if not found, insert it
  113.  
  114. ```javascript
  115. db.posts.update(
  116.   { name: "Shehan" },
  117.   { name: "Shay", age: 24 },
  118.   { upsert: true }
  119. );
  120. ```
  121.  
  122. #### Only update the specific part
  123.  
  124. ```javascript
  125. db.posts.update({ name: "Shehan" }, { $set: { name: "Morgan", isMale: true } });
  126. ```
  127.  
  128. #### Increase age by 2
  129.  
  130. ```javascript
  131. db.posts.update({ name: "Shehan" }, { $inc: { age: 2 } });
  132. ```
  133.  
  134. #### Rename 'age' to 'years'
  135.  
  136. ```javascript
  137. db.posts.update({ name: "Shehan" }, { $rename: { age: years } });
  138. ```
  139.  
  140. #### Remove an instance
  141.  
  142. ```javascript
  143. db.posts.remove({ name: "Shehan" });
  144. ```
  145.  
  146. #### Add sub documents
  147.  
  148. ```javascript
  149. db.posts.update({ name: "Shehan" }, { $set: { skills: ["JavaScript"] } });
  150. ```
  151.  
  152. #### Find a document by sub document
  153.  
  154. ```javascript
  155. db.users.find({ comments: { $elemMatch: { name: "Haily" } } }).pretty();
  156. ```
  157.  
  158. #### Creating an index
  159.  
  160. ```javascript
  161. db.users.createIndex({ username: "text" });
  162. ```
  163.  
  164. #### Searching with Index
  165.  
  166. ```javascript
  167. db.users.find({ $text: { $search: '"Moriah"' } }).pretty();
  168. ```
  169.  
  170. #### Greater than
  171.  
  172. ```javascript
  173. db.users.find({ id: { $gt: 9 } }).pretty();
  174. ```
  175.  
  176. #### Greater than or Equal
  177.  
  178. ```javascript
  179. db.users.find({ id: { $gte: 9 } }).pretty();
  180. ```
  181.  
  182. #### Lesser than
  183.  
  184. ```javascript
  185. db.users.find({ id: { $lt: 9 } }).pretty();
  186. ```
  187.  
  188. #### Lesser than or Equal
  189.  
  190. ```javascript
  191. db.users.find({ id: { $lte: 9 } }).pretty();
  192. ```
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top