I'm trying to output a Class (from the Classes Model) and its referenced Students--to learn mongoengine and mongodb. The below code gives me the error.
Expected 'pipeline' to be BSON docs (or equivalent), but got []
I'm sure it's something obvious––to those who know mongo and mongoengine. Any help (or push in the right direction) is appreciated :) Thanks in advance
import urllib
from mongoengine import *
connect(db=DB_NAME, username=DB_USER, password=DB_PASSWORD,
host=DB_URI)
class Students(Document):
student_id = IntField(unique=True)
name = StringField(max_length=50)
age = IntField(max_length=2)
gender = StringField(choices=('male', 'female'))
class Classes(Document):
class_id = IntField(required=True, unique=True) # 1576407600000
student_roster = ListField(ReferenceField(Students))
Students.objects.insert([
Students(name="John", student_id=425736, age=10, gender="male"),
Students(name="Mary", student_id=114391, age=9, gender="female")
])
Classes(class_id=1576407600000, student_roster=[425736, 114391]).save()
# gives pipeline error
c = Classes.objects.aggregate([
{'$lookup': {'from': 'students',
'localField' : 'student_roster',
'foreignField' : 'student_id',
'as': 'studentData'
}
}
])
list(c)