AutomEng
AutomEng

Reputation: 455

How to add documents to an array in a collection using PyMongo

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

Upvotes: 2

Views: 1428

Answers (1)

Saleem
Saleem

Reputation: 9008

Well, All I can suggest that make sure you have a document in your collection whose _id is equals to row[0]. If this document doesn't exists, there will be no effect of update_one.

I have modified code little bit (mostly how to get database and collection) and it looks like this.

from pymongo import MongoClient

mongoServer = "mongodb://localhost:27017"
mongoDb = "moviesDat"
mongoCol = "userDat"

client = MongoClient(mongoServer)
db = client.get_database(mongoDb)
col = db.get_collection(mongoCol)
rattings = []

# Data generation for testing purpose.    
# for i in range(1, 4):
#     rattings.append([0, i*1, i*2, i*3])

# assuming rattings is a valid collection with valid data
for row in rattings:
    col.update_one({"_id": row[0]}, {"$push": {"rattings": {"ratting": row[1], "movie_id": row[2], "timestamp": row[3]}}})

I can see it's pushing rows to collection.

userDat collection should look like:

{ 
    "_id" : 0.0, 
    "producer" : "james", 
    "rattings" : [
        {
            "timestamp" : 3, 
            "movie_id" : 2, 
            "ratting" : 1
        }, 
        {
            "timestamp" : 6, 
            "movie_id" : 4, 
            "ratting" : 2
        }, 
        {
            "timestamp" : 9, 
            "movie_id" : 6, 
            "ratting" : 3
        }  
    ]
}

Upvotes: 1

Related Questions