Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #### Show All Created Databases
- ```javascript
- show dbs
- ```
- #### Starte using the database named `dbname`
- ```javascript
- use dbname
- ```
- #### Show a collections inside a database
- ```javascript
- show collections
- ```
- #### Delete/Drop a database
- ```javascript
- db.dropDatabase();
- ```
- #### Create database if not exist and start using it
- ```javascript
- use dbname
- ```
- #### Get the current databse
- ```javascript
- db;
- ```
- #### Create a collection named `posts`
- ```javascript
- db.createCollection("posts");
- ```
- #### Insert a post into `posts` collection
- ```javascript
- db.posts.insert({ name: "Shehan", age: 24 });
- ```
- #### Insert many posts into `posts` collection
- ```javascript
- db.posts.insertMany([{}, {}, {}]);
- ```
- #### Get all the posts
- ```javascript
- db.posts.find();
- ```
- #### Show all posts in a pretty way
- ```javascript
- db.posts.find().pretty();
- ```
- #### Sort all posts based on title to Ascending order
- ```javascript
- db.posts.sort({ title: 1 }).pretty();
- ```
- #### Count the number os posts
- ```javascript
- db.posts.find({ category: "news" }).count();
- ```
- #### Get only 2 posts (Limit the number of posts)
- ```javascript
- db.posts
- .find()
- .limit(2)
- .pretty();
- ```
- ##### Get only 2 posts from the sorted posts
- ```javascript
- db.posts
- .find()
- .sort({ title: -1 })
- .limit(2)
- .pretty();
- ```
- #### Print all the names of posts
- ```javascript
- db.posts.find().forEach(function(post) {
- print("Post" + post.name);
- });
- ```
- #### Get only one post from 'news' category
- ```javascript
- db.posts.findOne({ category: "news" });
- ```
- #### Update a post, if not found, insert it
- ```javascript
- db.posts.update(
- { name: "Shehan" },
- { name: "Shay", age: 24 },
- { upsert: true }
- );
- ```
- #### Only update the specific part
- ```javascript
- db.posts.update({ name: "Shehan" }, { $set: { name: "Morgan", isMale: true } });
- ```
- #### Increase age by 2
- ```javascript
- db.posts.update({ name: "Shehan" }, { $inc: { age: 2 } });
- ```
- #### Rename 'age' to 'years'
- ```javascript
- db.posts.update({ name: "Shehan" }, { $rename: { age: years } });
- ```
- #### Remove an instance
- ```javascript
- db.posts.remove({ name: "Shehan" });
- ```
- #### Add sub documents
- ```javascript
- db.posts.update({ name: "Shehan" }, { $set: { skills: ["JavaScript"] } });
- ```
- #### Find a document by sub document
- ```javascript
- db.users.find({ comments: { $elemMatch: { name: "Haily" } } }).pretty();
- ```
- #### Creating an index
- ```javascript
- db.users.createIndex({ username: "text" });
- ```
- #### Searching with Index
- ```javascript
- db.users.find({ $text: { $search: '"Moriah"' } }).pretty();
- ```
- #### Greater than
- ```javascript
- db.users.find({ id: { $gt: 9 } }).pretty();
- ```
- #### Greater than or Equal
- ```javascript
- db.users.find({ id: { $gte: 9 } }).pretty();
- ```
- #### Lesser than
- ```javascript
- db.users.find({ id: { $lt: 9 } }).pretty();
- ```
- #### Lesser than or Equal
- ```javascript
- db.users.find({ id: { $lte: 9 } }).pretty();
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement