Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 2. Reading and Writing to MongoD... > Reading, Counting, and Sorting Docum...

2.7. Reading, Counting, and Sorting Documents in a Collection

In many situations, you only want to retrieve a single document from a collection. This is especially true when documents in your collection are unique on some property. A good example of this is a users collection, where each username is guaranteed unique.

# Assuming we already have a database handle in scope named dbh
# find a single document with the username "janedoe".
user_doc = dbh.users.find_one({"username" : "janedoe"})
if not user_doc:
    print "no document found for username janedoe"

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial