Subscribe to RSS feed  Follow @jackvamvas - Twitter

*Use the Comments section for questions Links



How to use MongoDB queries to increase your DBA expertise

15 June,2021 by Rambler

Question: We've started managing a monitoring system with an API interface -  generating output from a MongoDB database. We have no knowledge of MongoDB and just need a quick primer on some basics . In the meanwhile we are organising some training .


Answer: I received this request from some colleagues - and I'm finding as mindshare moves towards MongoDB there is a gap in DBA's knowledge on the nosql paradigm. But you have to start somewhere , and my recommendation was to download one of the MongoDB Atlas databases - e.g sample_airbnb - and play around with some of the commands. 



--return all Collections in db

--return all rows in a collection


From the MongoDB documentation "The aggregation pipeline is a framework for data aggregation modeled on the concept of data processing pipelines. Documents enter a multi-stage pipeline that transforms the documents into aggregated results"

--return only 2 columns - example of using an aggregate
db.listingsAndReviews.aggregate({$project : {_id : 1 , property_type : 1}}).pretty();
db.listingsAndReviews.aggregate({$project : {_id : 1 , calendar_last_scraped : 1}})
db.listingsAndReviews.aggregate({$project : {_id : 1 , calendar_last_scraped : 1}}).pretty();

--return a specific row with a value 
db.listingsAndReviews.find({_id :{ $eq:"10091713"}}).pretty();

db.listingsAndReviews.find({"name": /Rib/});

A  MongoDB view is the result of a stored aggregation on a collection, which database users can query just as if it were a materialized collection object.Some benefits include simplify coding
and decouple db & app

Views typically are the result of groupings and aggregation pipeline Data aggregations provide a summary of the underlying doc & collections

--added 7 days using the aggregate function
db.listingsAndReviews.aggregate({$project : {_id : 1 , calendar_last_scraped : 1, Plus_7_days :{ $add: [ "$calendar_last_scraped", 7*24*60*60000 ] }}})

--create a view
db.createView("PlusDays","listingsAndReviews",{$project : {_id : 1 , calendar_last_scraped : 1, Plus_7_days :{ $add: [ "$calendar_last_scraped", 7*24*60*60000 ] }}});

--view contents of collection

--get count

--drop the PlusDays view (collection)


I'll add some more variations tommorow

Author: Rambler (


Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.


Post a comment on How to use MongoDB queries to increase your DBA expertise