Skip to content

Instantly share code, notes, and snippets.

@mojaray2k
Last active May 15, 2020 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mojaray2k/9f1f0c5e1880391ab631905dead30e30 to your computer and use it in GitHub Desktop.
Save mojaray2k/9f1f0c5e1880391ab631905dead30e30 to your computer and use it in GitHub Desktop.
Mongo DB Queries

Queries used to interact with MongoDB

We are going to use a sample books collection

  1. Get all documents in a collection
  db.getCollection('books').find({})

reasult:

{
    "_id" : ObjectId("5ebda0bf1229b4654e39568b"),
    "title" : "First book title",
    "description" : "First book description",
    "price" : 11.0
}

/* 2 */
{
    "_id" : ObjectId("5ebda0bf1229b4654e39568c"),
    "title" : "Second book title",
    "description" : "Second book description",
    "price" : 22.0
}

/* 3 */
{
    "_id" : ObjectId("5ebda0bf1229b4654e39568d"),
    "title" : "Third book title",
    "description" : "Third book description",
    "price" : 33.0
}
  1. Get all documents in a collection where the price is greater than 15.
  db.getCollection('books').find({
    price: {$gt: 15}
 })

reasult:

/* 1 */
{
    "_id" : ObjectId("5ebda0bf1229b4654e39568c"),
    "title" : "Second book title",
    "description" : "Second book description",
    "price" : 22.0
}

/* 2 */
{
    "_id" : ObjectId("5ebda0bf1229b4654e39568d"),
    "title" : "Third book title",
    "description" : "Third book description",
    "price" : 33.0
}
  1. Update an existing record
db.books.update(
  {"_id" : ObjectId("5ebda0bf1229b4654e39568c")},
  {$set: {"title": "This title was updated" }}
)

result:

{
    "_id" : ObjectId("5ebda0bf1229b4654e39568c"),
    "title" : "This title was updated",
    "description" : "Second book description",
    "price" : 22.0
}

Find those books where the price is greater than 20 and add a discount of 55

db.books.update(
  {"price":{$gt: 20}},
  {$set: {"discount": 55}},
  {multi: true}
)

result

/* 1 */
{
    "_id" : ObjectId("5ebda0bf1229b4654e39568b"),
    "title" : "First book title",
    "description" : "First book description",
    "price" : 11.0
}

/* 2 */
{
    "_id" : ObjectId("5ebda0bf1229b4654e39568c"),
    "title" : "This title was updated",
    "description" : "Second book description",
    "price" : 22.0,
    "discount" : 55.0
}

/* 3 */
{
    "_id" : ObjectId("5ebda0bf1229b4654e39568d"),
    "title" : "Third book title",
    "description" : "Third book description",
    "price" : 33.0,
    "discount" : 55.0
}

MongoDB will also create new records if query criteria is met even though there might now be any matching data to update current records it has

db.books.update(
  {"_id" : ObjectId("5ebda0bf1229b4654e39567c")},
  {$set: {"title": "This title doesn't exists" }},
  {upsert:true}
)

result

/* 1 */
{
    "_id" : ObjectId("5ebda0bf1229b4654e39568b"),
    "title" : "First book title",
    "description" : "First book description",
    "price" : 11.0
}

/* 2 */
{
    "_id" : ObjectId("5ebda0bf1229b4654e39568c"),
    "title" : "This title was updated",
    "description" : "Second book description",
    "price" : 22.0,
    "discount" : 55.0
}

/* 3 */
{
    "_id" : ObjectId("5ebda0bf1229b4654e39568d"),
    "title" : "Third book title",
    "description" : "Third book description",
    "price" : 33.0,
    "discount" : 55.0
}

/* 4 */
{
    "_id" : ObjectId("5ebda0bf1229b4654e39567c"),
    "title" : "This title doesn't exists"
}

Delete a record in MongoDB by passing in the ObjectId to the remove method

db.books.remove(
  {"_id" : ObjectId("5ebda0bf1229b4654e39567c")}
)

result

/* 1 */
{
    "_id" : ObjectId("5ebda0bf1229b4654e39568b"),
    "title" : "First book title",
    "description" : "First book description",
    "price" : 11.0
}

/* 2 */
{
    "_id" : ObjectId("5ebda0bf1229b4654e39568c"),
    "title" : "This title was updated",
    "description" : "Second book description",
    "price" : 22.0,
    "discount" : 55.0
}

/* 3 */
{
    "_id" : ObjectId("5ebda0bf1229b4654e39568d"),
    "title" : "Third book title",
    "description" : "Third book description",
    "price" : 33.0,
    "discount" : 55.0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment