Mongo commands

Databases

// show databases
show dbs;
 
// use database
use cooker;

Collections

// show collections
show collections;
 
// current collection name
db.getName();

Query

find All

db.recipes.find();

prettify

db.recipes.find().pretty();

find by condition (exact equals)

db.recipes.find({
  title: "Tacos",
});
db.recipes.find({
  title: "Tacos",
  cook_time: 20,
});

find all but include one field in results

db.recipes.find(
  {},
  {
    title: 1,
  }
);

find all but exclude one field in results

db.recipes.find(
  {},
  {
    title: 0,
  }
);

find using regex pattern match

db.recipes.find({
  title: {
    $regex: /taco/i,
  },
});

limit max number of documents

db.recipes.find().limit(2);

sort b ascending

db.recipes.find().sort({
  title: 1, // -1 for descending
});

skip first document

db.recipes.find().skip(1);

Query Operators

cook time less than 30 mins

db.recipes.find(
  {
    cook_time: { $lte: 30 },
  },
  { title: 1 }
);

and condition: use simply comma

db.recipes.find(
  {
    cook_time: { $lte: 30 },
    prep_time: { $lte: 10 },
  },
  { title: 1 }
);

or condition

db.recipes.find(
  {
    $or: [
      {
        cook_time: { $lte: 30 }
      },
      {
        prep_time: { $lte: 10 }
      }
    ]
  },
  {
    title: 1,
    cook_time: 1,
    prep_time: 1
  }
);

search for one array value

  • Note: finding an array by one value also includes those documents where key does not exist/does not have value (maybe null?)
  • To remedy you can use $all with single object array
db.recipes.find(
  {
    tags: "easy"
  },
  {
    title: 1,
    tags: 1
  }
);

search for multiple array value (and condition)

db.recipes.find(
  {
    tags: {
      $all: [
        "easy",
        "quick"
      ]
    }
  },
  {
    title: 1,
    tags: 1
  }
);

search for multiple array value (or condition)

db.recipes.find(
  {
    tags: {
      $in: [
        "easy",
        "quick"
      ]
    }
  },
  {
    title: 1,
    tags: 1
  }
);

dot notation

db.recipes.find(
  {
    "ingredients.name": "egg"
  },
  {
    title: 1,
    ingredients: 1
  }
);
  • following is wrong!, since it will perform an exact object match on ingredients key value
db.recipes.find(
  {
    ingredients: {
      name: "egg"
    }
  },
  {
    title: 1,
    ingredients: 1
  }
);

Update and Delete

Update operators

update

db.examples.updateOne(
  {
    title: "Pizza",
  },
  {
    $set: {
      title: "Thin Crust Pizza",
    },
  }
);

update and add/set a field

db.examples.updateOne(
  {
    title: "Thin Crust Pizza",
  },
  {
    $set: {
      vegan: false,
    },
  }
);

update and delete/unset a field

db.examples.updateOne(
  {
    title: "Thin Crust Pizza",
  },
  {
    $unset: {
      vegan: 1,
    },
  }
);

update and increment/decrement a field value

db.examples.updateOne(
  {
    title: "Thin Crust Pizza",
  },
  {
    $inc: {
      likes: 1, // -1 for decrement
    },
  }
);

update an array by adding objects in it

db.examples.updateOne(
  {
    title: "Tacos"
  },
  {
    $push: {
      likes: 60  // likes is an array containing userIds
    }
  }
);

update an array by removing objects in it

db.examples.updateOne(
  {
    title: "Tacos"
  },
  {
    $pull: {
      likes: 60  // likes is an array containing userIds
    }
  }
);

delete document

db.examples.deleteOne({
  _id: ObjectId("6047a04c7be8d9017e67dca7"),
});

explain the query

  • executionStats.totalDocsExamined shows how many docs the query went thru
db.recipes.find(
  {
    cook_time: 10
  },
  {
    title: 1
  }
).explain("executionStats");

Indexes

get indexes

  • default index is _id which is created automatically
db.recipes.getIndexes();

create index in descending order

db.recipes.createIndex(
  {
    cook_time: -1 // 1 for ascending order
  }
);

delete index by index name

  • index name can be retrieved by result of getIndexes()
db.recipes.dropIndex("cook_time_-1");

Misc

Get datatype of the field

{
"fieldType": {  "$type": "$creationDateTime"  }
}