1919
2020sys .path [0 :0 ] = ["" ]
2121
22+ import pymongo
2223from test import client_context , unittest
2324
2425
@@ -30,6 +31,10 @@ def setUpClass(cls):
3031 # Run once before any tests run.
3132 client_context .client .pymongo_test .inventory .drop ()
3233
34+ @classmethod
35+ def tearDownClass (cls ):
36+ client_context .client .drop_database ("pymongo_test" )
37+
3338 def tearDown (self ):
3439 # Run after every test.
3540 client_context .client .pymongo_test .inventory .drop ()
@@ -682,8 +687,8 @@ def insert_docs():
682687
683688 # Start Changestream Example 4
684689 pipeline = [
685- {"$match" :
686- { "$or" : [
690+ {"$match" : {
691+ "$or" : [
687692 {"fullDocument.username" : "alice" },
688693 {"operationType" : {"$in" : ["delete" ]}}]
689694 }
@@ -696,6 +701,118 @@ def insert_docs():
696701 done = True
697702 t .join ()
698703
704+ def test_aggregate_examples (self ):
705+ db = client_context .client .pymongo_test
706+
707+ # Start Aggregation Example 1
708+ db .sales .aggregate ([
709+ {"$match" : {"items.fruit" : "banana" }},
710+ {"$sort" : {"date" : 1 }}
711+ ])
712+ # End Aggregation Example 1
713+
714+ # Start Aggregation Example 2
715+ db .sales .aggregate ([
716+ {"$unwind" : "$items" },
717+ {"$match" : {"items.fruit" : "banana" }},
718+ {"$group" : {
719+ "_id" : {"day" : {"$dayOfWeek" : "$date" }},
720+ "count" : {"$sum" : "$items.quantity" }}
721+ },
722+ {"$project" : {
723+ "dayOfWeek" : "$_id.day" ,
724+ "numberSold" : "$count" ,
725+ "_id" : 0 }
726+ },
727+ {"$sort" : {"numberSold" : 1 }}
728+ ])
729+ # End Aggregation Example 2
730+
731+ # Start Aggregation Example 3
732+ db .sales .aggregate ([
733+ {"$unwind" : "$items" },
734+ {"$group" : {
735+ "_id" : {"day" : {"$dayOfWeek" : "$date" }},
736+ "items_sold" : {"$sum" : "$items.quantity" },
737+ "revenue" : {
738+ "$sum" : {
739+ "$multiply" : [
740+ "$items.quantity" , "$items.price" ]
741+ }
742+ }
743+ }
744+ },
745+ {"$project" : {
746+ "day" : "$_id.day" ,
747+ "revenue" : 1 ,
748+ "items_sold" : 1 ,
749+ "discount" : {
750+ "$cond" : {
751+ "if" : {"$lte" : ["$revenue" , 250 ]},
752+ "then" : 25 ,
753+ "else" : 0
754+ }
755+ }
756+ }
757+ }
758+ ])
759+ # End Aggregation Example 3
760+
761+ # $lookup was new in 3.2. The let and pipeline options
762+ # were added in 3.6.
763+ if client_context .version .at_least (3 , 6 , 0 ):
764+ # Start Aggregation Example 4
765+ db .air_alliances .aggregate ([
766+ {"$lookup" : {
767+ "from" : "air_airlines" ,
768+ "let" : {"constituents" : "$airlines" },
769+ "pipeline" : [
770+ {"$match" : {"$expr" : {"$in" : ["$name" , "$$constituents" ]}}}
771+ ],
772+ "as" : "airlines"
773+ }
774+ },
775+ {"$project" : {
776+ "_id" : 0 ,
777+ "name" : 1 ,
778+ "airlines" : {
779+ "$filter" : {
780+ "input" : "$airlines" ,
781+ "as" : "airline" ,
782+ "cond" : {"$eq" : ["$$airline.country" , "Canada" ]}
783+ }
784+ }
785+ }
786+ }
787+ ])
788+ # End Aggregation Example 4
789+
790+ def test_commands (self ):
791+ db = client_context .client .pymongo_test
792+ db .restaurants .insert_one ({})
793+
794+ # Start runCommand Example 1
795+ db .command ("buildInfo" )
796+ # End runCommand Example 1
797+
798+ # Start runCommand Example 2
799+ db .command ("collStats" , "restaurants" )
800+ # End runCommand Example 2
801+
802+ def test_index_management (self ):
803+ db = client_context .client .pymongo_test
804+
805+ # Start Index Example 1
806+ db .records .create_index ("score" )
807+ # End Index Example 1
808+
809+ # Start Index Example 1
810+ db .restaurants .create_index (
811+ [("cuisine" , pymongo .ASCENDING ), ("name" , pymongo .ASCENDING )],
812+ partialFilterExpression = {"rating" : {"$gt" : 5 }}
813+ )
814+ # End Index Example 1
815+
699816 @client_context .require_version_min (3 , 6 , 0 )
700817 @client_context .require_replica_set
701818 def test_misc (self ):
0 commit comments