Skip to content

Latest commit

 

History

History
101 lines (73 loc) · 2.62 KB

mongo.md

File metadata and controls

101 lines (73 loc) · 2.62 KB

Database CheatSheet

MongoDB CheatSheet

Connect & Backup

mongodb+srv://<username>:<password>@<cluster>/test

mongosh "mongodb+srv://<cluster>/myFirstDatabase" --apiVersion 1 --username mongo

mongosh "mongodb+srv://<username>:<password>@<cluster>/test" --apiVersion 1

mongosh "mongodb+srv://<username>:<password>@<cluster>/sample_airbnb" --apiVersion 1

mongodump --uri "mongodb+srv://<username>:<password>@<cluster>/sample_airbnb"

mongoexport --uri "mongodb+srv://<username>:<password>@<cluster>/sample_airbnb" --collection=listingsAndReviews --out=listingsAndReviews.json

Query

image

show dbs
use <databasename>
show collections
db.<collectionName>.find({"<fieldName>":"<fieldValue>"})
db.<collectionName>.count()
db.<collectionName>.find({"<fieldName>":"<fieldValue>"}).pretty()

Insert

db.zips.insert({
  city: 'HUEYTOWN',
  zip: '35023',
  loc: { y: 33.414625, x: 86.999607 },
  pop: 39677,
  state: 'AL'    
});
db.zips.insert({
  _id: Object("")
  city: 'HUEYTOWN',
  zip: '35023',
  loc: { y: 33.414625, x: 86.999607 },
  pop: 39677,
  state: 'AL'    
});
// insert multiple documents
db.zips.insert([{
  _id: Object("")
  city: 'HUEYTOWN',
  zip: '35023',
  loc: { y: 33.414625, x: 86.999607 },
  pop: 39677,
  state: 'AL'    
}, {...}]);
// ordered
db.pets.insert([{ "_id": 1, "pet": "cat" },
                { "_id": 1, "pet": "dog" },
                { "_id": 3, "pet": "fish" },
                { "_id": 4, "pet": "snake" }], { "ordered": false })
// => cat, finish, snake documents will be inserted

image

Update

MQL : MongoDB Query Language

image

// we can use this operator 
db.zips.updateMany({"city":"HUDSON"},{"$set": {"area":9876.54}});

Delete

  • Delete Document - db.{collectionName}.deleteOne({})/.deleteMany({})
  • Delete Collection - db.{collectionName}.drop();
  • Delete Database - drop

MQL Operators

image

Comparison Operators

image

Logic Operators

image