0

I have a collection of mongodb, which contains "MoviesID, UserID, Rating", so this is to describe the how the users rate different movies, and a user can rate different movies, and certainly a movie can be rated by different users. Now I want to find all the users who rate similar movies with target user. Rating similar movies means that they rate at least same movie, or more. My way is to find all the movies of target user, and then query for the users who also rate these movies. I have following mongo shell query, and it works:

var a = db.ratings.distinct("MovieID",{"UserID":"user"});
db.ratings.distinct("UserID",{"MovieID":{$in:a}}))

but because I want an input, i tried pymongo to get input, my code is:

import sys
import pymongo
import re
import codecs

from pymongo import MongoClient
client = MongoClient()

db = client['moviesdb']

user = input("Enter the user: ")

# #ratings = db.ratings

from bson.son import SON



import pprint

a = db.ratings.distinct("MovieID",{"UserID":"user"});
pprint.pprint(db.ratings.distinct("UserID",{"MovieID":{"$in":a}}));

but the result is an empty array, I do not know why. So what's the problem with the pymongo code? Or is there any way to get an input in shell so i do not need to write Pymongo? Thanks!

CPP
  • 91
  • 1
  • 12

1 Answers1

1

Simple pilot error. Change

a = db.ratings.distinct("MovieID",{"UserID":"user"});

to

a = db.ratings.distinct("MovieID",{"UserID":user});
Buzz Moschetti
  • 7,057
  • 3
  • 23
  • 33