Hi so I have this test data in mongo for mongoengine that I use for storing user's cart:
{
"_id" : ObjectId("55e492ac516ddc17a8b07d2a"),
"user" : ObjectId("55e3f236516ddc78296968be"),
"items" : [
{
"item" : ObjectId("55e24cd6516ddcbdc081842b"),
"quantity" : 2,
"added_date" : ISODate("2015-08-31T17:44:49.023Z")
},
{
"item" : ObjectId("55e24cd6516ddcbdc0818425"),
"quantity" : 3,
"added_date" : ISODate("2015-08-31T17:44:49.025Z")
},
{
"item" : ObjectId("55e24cd6516ddcbdc0818420"),
"quantity" : 3,
"added_date" : ISODate("2015-08-31T17:44:49.026Z")
}
]
}
Here the models:
class CartItem(mongoengine.EmbeddedDocument):
item = mongoengine.ReferenceField('Item')
quantity = mongoengine.IntField()
added_date = mongoengine.DateTimeField(default=datetime.now())
class Cart(mongoengine.Document):
user = mongoengine.ReferenceField('User')
items = mongoengine.EmbeddedDocumentListField(CartItem)
Here I store items in user's cart. Now I would like to get all the unique items in the items list field because there will be duplicate items.
I perform the following queries to get the items:
cart = Cart.objects.filter(user=user).first()
queryset = cart.items
In this case I think I would have to group the items, I tried using raw query in filter : cart.items.filter(__raw__...)
But this just doesn't work because raw is not supported in this case. Can someone please help me in how I can do this? Thank you!