Python operation of the underlying operation of MongoDB

Source: Internet
Author: User
Tags mongoclient

#coding: utf-8__author__ = ' HDFs ' import pymongofrom pymongo import mongoclientclient = Mongoclient () client=mongoclient ( ' ', 27017) #连接mongodb数据库client = mongoclient (' mongodb://') #指定数据库名称db = client.test_database# Get non-system collection Db.collection_names (include_system_collections=false) #获取集合名posts = db.posts# Find a single document Posts.find_one () # A document of a given condition Posts.find_one ({"Author": "Mike"}) #使用ID查找需要ObjectIDfrom Bson.objectid import objectidpost_id= ' 5728aaa96795e21b91c1aaf0 ' document = Client.db.collection.find_one ({' _id ': ObjectId (post_id)}) Import Datetimenew_ posts = [{"Author": "Mike", "text": "Another post!", "tags": ["Bulk", "insert"], "date             ": Datetime.datetime (one, one, one, one)}, {" Author ":" Eliot "," title ":" MongoDB is Fun ", "Text": "and Pretty Easy Too!", "date": Datetime.datetime (one, one, ten, Ten)}] #插入多条记录result =    Sert_many (new_posts) #返回插入的IDresult. inserted_ids# Recursive collection for post in Posts.find ():post# recursive condition set for post in Posts.find ({"Author": "Mike"}): Number of records for post# document Posts.count () #区间查询d = Datetime.datetime (2009, 11, 12, For post in Posts.find ({"date": {"$lt": D}}). Sort ("author"): Print post# index unique index to set profiles result = Eate_index ([' user_id ', Pymongo. Ascending)],unique=true) #查看索引信息list (Db.profiles.index_information ()) #user_profiles = [{' user_id ': 211, ' name ': ' Luke '},{' user_id ': 212, ' name ': ' ziltoid '}]result = Db.profiles.insert_many (user_profiles) #聚合查询from Pymongo Import Mongoclientdb = mongoclient (' mongodb://'). aggregation_example# Preparing Data result = Db.things.insert_many ([{  "X": 1, "tags": ["Dog", "Cat"]}, {"X": 2, "tags": ["Cat"]}, {"X": 2, "tags": ["Mouse", "Cat", "Dog"]}, {"X": 3, "tags": []}]) result.inserted_ids "{" _id ": ObjectId ("576aaa973e5269020848cc7c"), "X": 1, "tags": ["Dog", "Cat"]} {"_id": ObjectId ("576aaa973e5269020848cc7d"), "X": 2, "tags": ["Cat"]} {"_id": ObjectId ("576aaa973e5269020848cc7e"), "X": 2, "tags": ["Mouse", "Cat", "Dog"]} {"_id": ObjectId ("576aaa973e5269020848cc7f"), "X": 3, "tags": []} ' from Bson.son import son# $unwind untie-The following variable Pipelin e = [{"$unwind": "$tags"}, {"$group": {"_id": "$tags", "Count": {"$sum": 1}}, {"$sort": SON ([("Count",-1), ("_id",-1)])} ]list (Db.things.aggregate (pipeline)) #使用聚合函数with Commanddb.command (' aggregate ', ' things ', pipeline=pipeline, Explain=true)


Python operation of the underlying operation of MongoDB

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: 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.