-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate orders
40 lines (35 loc) · 1.02 KB
/
migrate orders
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Create a new collection called "old_orders" and populate it with a large number of documents representing old orders.
# Write a Python script to migrate these old orders to a new collection called "new_orders" while transforming the data structure
# and adding additional fields.
import pymongo
import datetime
import pprint
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
old_orders = [{
"orderId": 1,
"customer": "John Doe",
"item": "Laptop"
},{
"orderId": 2,
"customer": "Jane Smith",
"item": "Phone"
},{
"orderId": 3,
"customer": "Swara D",
"item": "TV"
}]
db = client["test"]
orders = db.old_orders
result = orders.insert_many(old_orders)
new_orders = db.new_orders
for order in orders.find():
new_order = {
"orderId": order['orderId'],
"customer_name": order['customer'],
"item": order['item'],
"migration_date": datetime.datetime.now()
}
new_orders.insert_one(new_order)
for order in new_orders.find():
pprint.pprint(order)