I have extracted data from a txt file into a NumPy array. I am now trying to add this data to an array of embedded documents in an already existing collection. Currently, the array is named "ratings" and holds only one document with empty fields.
Here is the code:
ratings = np.loadtxt('outfile_ratings.sql', skiprows=1)
fn = 'outfile_users.sql'
with open(fn, encoding="utf-8") as f: lines = f.readlines()
[l.strip().split("\t") for l in lines]
users = np.array([l.strip().split("\t") for l in lines])
dbClient = pm.MongoClient()
db = dbClient['moviesDat']
col = db['usersDat']
for i in range(1, 944):
if np.size(users[i][:]) == 5:
resInsert = col.insert_one({"_id": users[i][0]})
for i in range(1, 944):
if np.size(users[i][:]) == 5:
resUpdate = col.update_one({"_id": users[i][0]},
{"$set": {"age": users[i][1],
"gender": users[i][2],
"occupation": users[i][3],
"zip_code": users[i][4]}})
for row in ratings:
resUpdate = col.update_one({"_id": row[0]},
{"$addToSet": {"ratings": {"rating": " ",
"movie_id": " ",
"timestamp": " "}}})
for row in ratings:
resUpdate = col.update_one({"_id": str(row[0])},
{"$push": { "ratings": {"rating": row[2],
"movie_id": row[1],
"timestamp": row[3]}}})
In the final call to update_one() I am using the $push operator to add the values to the embedded document fields but to no effect.
How do I add the data to the array in my collection?
EDIT: ...and the data set files:
outfile_ratings.sql:
user movie rating timestamp
1 1 5 874965758
1 2 3 876893171
1 3 4 878542960
1 4 3 876893119
1 5 3 889751712
1 6 5 887431973
1 7 4 875071561
1 8 1 875072484
outfile_users.sql:
id age gender occupation zip_code
1 24 M technician 85711
2 53 F other 94043
3 23 M writer 32067
4 24 M technician 43537
5 33 F other 15213
6 42 M executive 98101
7 57 M administrator 91344