Welcome Coders

! Here is a every thing which is you want !

! Cheatsheet of every coding language is available on this website !


Mongo Cheatsheet

  1. Show All Databases
    show dbs
                        
  2. Show Current Database
    db
                        
  3. Create Or Switch Database
    use acme
                        
  4. Drop
    db.dropDatabase()
                        
  5. Create Collection
    db.createCollection('posts')
                        
  6. Show Collections
    show collections
                        
  7. Insert Row
    db.posts.insert({
    title: 'Post One',
    body: 'Body of post one',
    category: 'News',
    tags: ['news', 'events'],
    user: {
      name: 'John Doe',
      status: 'author'
    },
    date: Date()
    })
                        
  8. Insert Multiple Rows
    db.posts.insertMany([
    { title: 'Post Two',
    body: 'Body of post two',
    category: 'Technology',
    date: Date()
    },
    {
      title: 'Post Three',
      body: 'Body of post three',
      category: 'News',
      date: Date()
    },
    {
      title: 'Post Four',
      body: 'Body of post three',
      category: 'Entertainment',
      date: Date()
    }
    ])
                        
  9. Get All Rows
    db.posts.find()
                        
  10. Get All Rows Formatted
    db.posts.find().pretty()
    
                        
  11. Find Rows
    db.posts.find({ category: 'News' })
                        
  12. Find One Row
    db.posts.findOne({ category: 'News' })
                        
  13. Delete Row
    db.posts.remove({ title: 'Post Four' })
                        
  14. Add Index
    db.posts.createIndex({ title: 'text' })
                        
  15. Text Search
    db.posts.find({
      $text: {
        $search: "\"Post O\""
        }
    })
                        
  16. Greater and Less Than
    db.posts.find({ views: { $gt: 2 } })
    db.posts.find({ views: { $gte: 7 } })
    db.posts.find({ views: { $lt: 7 } })
    db.posts.find({ views: { $lte: 7 } })
                        
  17. Rename Field
    db.posts.update({ title: 'Post Two' },
    {
      $rename: {
        likes: 'views'
      }
    })
                        
  18. Find Specific Fields
    db.posts.find({ title: 'Post One' }, {
      title: 1,
      author: 1
    })
                        
  19. Chaining
    db.posts.find().limit(2).sort({ title: 1 }).pretty()
                        
  20. Count Rows
    db.posts.find().count()
    db.posts.find({ category: 'news' }).count()