Sunday, 1 December 2013

MondoDB Queries

The find() Method

To query data from MongoDB collection, you need to use MongoDB's find() method.

> db.democol.find()

Result:

{ "_id" : ObjectId("52963c2a6f63f810a98b7a98"), "title" : "Learn mongo", "by" :
"mani", "likes" : 100 }
{ "_id" : ObjectId("52963ce96f63f810a98b7a99"), "title" : "Learn SQL", "by" : "s
ubramanian", "likes" : 20, "comments" : [  {  "user" : "Tiara",  "message" : "Wo
rth to read" } ] }


The pretty() Method

To display the results in a formatted way, you can use pretty() method.

> db.democol.find().pretty()

Result:

{
        "_id" : ObjectId("52963c2a6f63f810a98b7a98"),
        "title" : "Learn mongo",
        "by" : "mani",
        "likes" : 100
}
{
        "_id" : ObjectId("52963ce96f63f810a98b7a99"),
        "title" : "Learn SQL",
        "by" : "subramanian",
        "likes" : 20,
        "comments" : [
                {
                        "user" : "Tiara",
                        "message" : "Worth to read"
                }
        ]
}


AND in MongoDB

In the find() method if you pass multiple keys by separating them by ',' then MongoDB treats it AND condition. Basic syntax of AND is shown below:

> db.democol.find({by:"mani"}).pretty()

Result:

> db.democol.find({by:"mani"}).pretty()
{
        "_id" : ObjectId("52963c2a6f63f810a98b7a98"),
        "title" : "Learn mongo",
        "by" : "mani",
        "likes" : 100
}

> db.democol.find({by:"mani",title:"Learn mongo"}).pretty()

Result:

{
        "_id" : ObjectId("52963c2a6f63f810a98b7a98"),
        "title" : "Learn mongo",
        "by" : "mani",
        "likes" : 100
}


OR in MongoDB

To query documents based on the OR condition, you need to use $or keyword. Basic syntax of OR is shown below:

> db.democol.find({$or:[{by:"mani"},{title:"Learn SQL"}]}).pretty()

Result:

{
        "_id" : ObjectId("52963c2a6f63f810a98b7a98"),
        "title" : "Learn mongo",
        "by" : "mani",
        "likes" : 100
}
{
        "_id" : ObjectId("52963ce96f63f810a98b7a99"),
        "title" : "Learn SQL",
        "by" : "subramanian",
        "likes" : 20,
        "comments" : [
                {
                        "user" : "Tiara",
                        "message" : "Worth to read"
                }
        ]
}

DBT - Models

Models are where your developers spend most of their time within a dbt environment. Models are primarily written as a select statement and ...