Add MongoDB support for Python under Windows platform Pymongo

Source: Internet
Author: User
Tags mongoclient mongodb support

    1. Download Pymongo-2.6.3.win-amd64-py2.7.exe on the Python website

    2. Installing Pymongo-2.6.3.win-amd64-py2.7.exe

    3. Test with the official use case

  • Open a command prompt and enter the Python runtime environment.

  • Import Pymongo Module

    >>> Import Pymongo

  • Establish a link to the local MongoDB service

  • >>> client = Pymongo. Mongoclient ("localhost", 27017)

  • Connect the test Database

  • >>> db = Client.test

  • Query the database name of the connection

  • >>> Db.nameu ' Test '

  • Querying My_collection Collection Information

  • >>> db.my_collectioncollection (Database (mongoclient (' localhost ', 27017), U ' Test '), U ' my_collection ')

  • Add some test documents/objects to the My_collection collection

  • >>> Db.my_collection.save ({"X": Ten}) ObjectId (' 530034752052d502c4a250aa ') >>> Db.my_ Collection.save ({"X": 8}) ObjectId (' 5300347d2052d502c4a250ab ') >>> db.my_collection.save ({"X": 11}) ObjectId (' 530034832052d502c4a250ac ')

  • Querying a document/object in the My_collection collection

  • >>> Db.my_collection.find_one () {u ' x ': Ten, U ' _id ': ObjectId (' 530034752052D502C4A250AA ')}

  • Queries all documents/objects in the My_collection collection and iterates through the output

  • Indentationerror:expected an indented block>>> for item in Db.my_collection.find (): ... print item["x"] ... 10811

  • Create an index for the My_collection collection

    >>> Db.my_collection.create_index ("x") u ' x_1 '
  • Queries all documents/objects in the My_collection collection and traverses the output in ascending order

    >>> for item in Db.my_collection.find (). Sort ("X", Pymongo. Ascending): ... print item["x"] ... 81011
  • Queries all documents/objects in the My_collection collection, and certain rules traverse the output

    >>> [item["x"] for item in Db.my_collection.find (). Limit (2). Skip (1)][8, 11]

Add MongoDB support for Python under Windows platform Pymongo

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.