MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

PyMongo:come utilizzare l'aggregato e archiviare i risultati in un'altra raccolta?

Di seguito è riportato il codice funzionante testato su pymongo versione 3.6.1 e python 3.6.4

    import pymongo
    from pymongo import MongoClient
    client = MongoClient('127.0.0.1')  # mongodb running locally
    dbRead = client['test']            # using the test database in mongo
    # create the pipeline required 
    pipeline = [{"$match": {"$text": {"$search":"StackOverflow"}}},{"$out":"C_b"}]  # all attribute and operator need to quoted in pymongo
    dbRead.C_a.aggregate(pipeline)  #execution 
    print (dbRead.C_b.count()) ## verify count of the new collection