Skip to content

Instantly share code, notes, and snippets.

@sunil-bagde
Created February 3, 2021 16:27
Show Gist options
  • Save sunil-bagde/55f808264e9838bbb668dab8c661eabe to your computer and use it in GitHub Desktop.
Save sunil-bagde/55f808264e9838bbb668dab8c661eabe to your computer and use it in GitHub Desktop.

Databases

MongoDB query

ACID Property

https://en.wikipedia.org/wiki/ACID

pets == collection

show all databases;

show dbs;

create or switch to databases;

use adaptation

insert a new records

  db.pets.insertOne({
    name:"",
    type:"",
    breed:"",
    age:""
  })

counts a records

db.pets.count()

Stats

it lits
collections
db name,
db.stats();

find

db.pets.findOne()
db.pets.findOne({
  type:"Dog"
})

if it's finds return document or return NULL

find all

db.pets.fins({
  type: "dog"
})

insert many

db.pets.insertMany([
{
 name:"",
 type:"",
 breed:"",
 age:""
},
{
 name:"",
 type:"",
 breed:"",
 age:""
},
])

find by index

db.pets.findOne({index: 1233});

limits a records

db.pets.find({}).limit(5)

to array

db.pets.find({type: "dog"}).limit(40).toArray()  ;

Query

Comparison

greater than $gt $eq $gte $lt $lte

$in $i

db.pets.count({
  type: "cat",
  age: { $gt: 12 }
})

$nin

db.pets.find({age: { $in: [3,8]}});

select all document in the pets collection where value

either 5 or 15

Logical operators

https://docs.mongodb.com/manual/reference/operator/query-logical/

$and $not $nor $or

db.pets.count({
 type: "bird",
 $and: [{
  age: {$gte: 4},
  age: {$lte: 8}
 }]
})

Sort

db.pets.find({
  type:"Dog",
}).sort({
  age: -1 // DESC
}).limit(5);

Projection operator

// Select specific pluck

select only name

db.pets.fins({type:dog},{name:1, _id: -1}).limits()

updating MongoDB

db.collection.updateOne(<query>,<update-data>)
db.pets.updateOne(
{
  $type: "",
  name: "",
  breed: ""
},
{$set: {owner : ""}}
)

update many

db.pets.updateMany(
{type: "dog"}.
{$inc: {age: 1}}
);

Upsert

db.pets.updateOne(
{
type: "dog",
name: "Sudo",
breed: "Wheaten"
},

{

 $set: {

 type: "dog",
 name: "Sudo",
 breed: "Wheaten"
 age: 5,
 index : 44553,
 owner: "Sara Drasner"
},

{

 upsert : true

}



)

Delete

db.pets.deleteMany({
  type: "reptile",
  breed: "Havanese"
})

findOne and Delete return a result and delete

 db.pets.findOneDelete(
 {name: "Fido"},
 {type: "reptile"}
 )

bulkWrite

series of query all at once.

explain

 db.pets.find({
  name : ""
 }).explain("execuationStats")

Index

create index

db.pets.createIndex(
{
  name: 1
}
)

get indexes

db.pets.getIndexes();

unique index

db.pets.createIndex({index: 1}, {unique: true})

full text search

Collection only support

one text index

db.pets.createIndex({

 type: "text",
 breed: "text",
 name: "text"
})

Query

 db.pets.find(
 { $text: {$search: "dog havanese luna"} }
 )

Top score

  db.pets.fins(
  {$text: {$search: "dog havanese luna"}}
  ).sort(
  {score: {$meta: "textScore" }}
  )

Aggregation

db.pets.aggregate([
  {
    $bucket: {
      groupBy: "$age",
      boundaries: [0, 3, 9, 15],
      default: "very senior",
      output: {
        count: { $sum: 1 },
      },
    },
  },
]);

match

db.pets.aggregate(
{
$match: {
 type: "dog"
},
{
  $bucket: {
    groupBy: "$age",
    boundaries: [0, 3, 9, 15],
    default: "very senior",
    output: {
      count: { $sum: 1 },
    },
  },
},
);

sort

db.pets.aggregate(
{
$match: {
 type: "dog"
},
{
  $bucket: {
    groupBy: "$age",
    boundaries: [0, 3, 9, 15],
    default: "very senior",
    output: {
      count: { $sum: 1 },
    },
  },
  $sort: {
    count: -1
  }
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment