Showing posts with label Find. Show all posts
Showing posts with label Find. Show all posts

Friday, 4 January 2019

How to Query on MongoDB by _id - INTERMEDIATE II


What if we have to query on MongoDB collections based on the "_id" field, Can we really query on "_id" field ? If so, what is the syntax ? Let's try this out -

Let's first fetch a document's id -

$ db.user.find_one()

{'_id': ObjectId('5c16e863817810ed3fc5e5f9'),
 'Fname': 'atul',
 'Lname': 'Singh',
 'Grade': 12.0,
 'College': 'SGM',
 'Job': 'Student',
 'Address': 'Young St.'}



Now, we will pick the object id and use this to fetch the same document from collection

$ db.user.find_one({'_id':'5c16e863817810ed3fc5e5f9'}) #this will return nothing

$ db.user.find_one({'_id':ObjectId('5c16e863817810ed3fc5e5f9')})
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
 in 
----> 1 db.user.find_one({'_id':ObjectId('5c16e863817810ed3fc5e5f9')})

NameError: name 'ObjectId' is not defined


We have received this error because ObjectId is not the same as its string representation, it must be converted to ObjectId from a string before it is passed to find command.

$ from bson.objectid import ObjectId
$ db.user.find_one({'_id':ObjectId('5c16e863817810ed3fc5e5f9')})

{'_id': ObjectId('5c16e863817810ed3fc5e5f9'),
 'Fname': 'atul',
 'Lname': 'Singh',
 'Grade': 12.0,
 'College': 'SGM',
 'Job': 'Student',
 'Address': 'Young St.'}



IPYTHON Notebook can be found HERE








Like the below page to get the update  
Facebook Page      Facebook Group      Twitter Feed      Google+ Feed      Telegram Group     


Wednesday, 2 January 2019

How to iterate MongoDB Cursor in Python - Intermediate I


Whenever querying on mongodb, always store the output into a variable, called cursor, before performing any operation on data. It will keep your data into variable without messing your output ground. PyMongo Cursor variable supports few functions which helps with few information without actual seeing your data such as retrieved data count or distinct values in particular key etc.

In this session, we will learn about the mongo db cursor variables, for this exercise also we are going to use 'USER' database hosted on free tier MongoDB Atlas (M0) Server.


Jupyter Notebook can be accessed HERE also

= =


Next Post on this Series and more on MongoDB can be find here -> LINK





Like the below page to get the update  
Facebook Page      Facebook Group      Twitter Feed      Google+ Feed      Telegram Group     


Monday, 31 December 2018

MongoDB with Python - Basics III - Find/Select Operation


Hoping you guys are enjoying the NoSql journey so far (previous posts links), till now we have seen basic CRUD operation. From this post onward, I am diving in details of these operations and starting with FIND or SELECT operation in MongoDB. We will learn what are the ways and options provided by MongoDB to select or project the data.

When you start working with complex queries you might {as I have said "Might"} face difficulties with tracking of braces {([ as I've experienced the same with me/my team/students and colleagues. But no worries, Jupyter Notebook provides a  couple highlighter for braces when selected or you can use notepad++ also (which I think is not so useful as you are not gonna copy/paste the syntax so frequently).

I advise everyone to avoid the writing queries directly on mongo shell prompt as it doesn't provide any intelligence and not so good in fixing queries if made mistake.

I am sure you will love this post as well and if have any question feel free to ask in comment section below.

For on Mongo DB -> Link


There are many more Find/Read Operators supported by MongoDB, Full list can be found HERE

CRUD Operation - (Read) : Link




Next Post on this Series and more on MongoDB can be find here -> LINK




Like the below page to get the update  
Facebook Page      Facebook Group      Twitter Feed      Google+ Feed      Telegram Group     


Saturday, 29 December 2018

MongoDB with Python - Basics II - CRUD Operations


In this post, we will learn about the Advance Find and Create Operations with Sort, Skip and Limit functionality. Pymongo driver support almost same kind of syntax for python which mongo shell used.
The benefit of python (or any programming language) + mongo is to use both langauge/db functionality to work with mongo. Though, to perform the same operation is faster then performing by python but it depends on the activity you are performing.



CRUD Operation - (Create, Read, Update & Delete) : Link




Next Post on this Series and more on MongoDB can be find here -> LINK




Like the below page to get the update  
Facebook Page      Facebook Group      Twitter Feed      Google+ Feed      Telegram Group