Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoengine reference field query

I'm building a reservation site for a restaurant using flask framework and mongoengine.

My main object is to fetch all the reservation objects which customer id's equal to wanted customer id with json

data = rzv.objects(restaurant=rest, customer=cdb.objects.get(id=request.args.get("customer-reservation"))).all()

When i try to trigger this query json gives me an error:

mongoengine.errors.InvalidQueryError

My Reservation model below:

class Reservations(document.Document):
    restaurant = fields.ReferenceField(Restaurant)
    customer = fields.ReferenceField(Customers)
    shift_type = fields.EmbeddedDocumentField(Shifts)
    room = fields.ReferenceField(Rooms)
    time = fields.StringField()
    covers = fields.IntField()
    status = fields.StringField(default="wait")
    desk = fields.EmbeddedDocumentField(Desks)
    date = fields.DateTimeField()
    sit_date = fields.DateTimeField()
    end_sit_date = fields.DateTimeField()
    cancel_date = fields.DateTimeField()

My Customer model below:

class Customers(document.Document):
    title = fields.StringField()
    full_name = fields.StringField()
    first_name = fields.StringField()
    last_name = fields.StringField()
    telephone = fields.StringField()
    visits = fields.StringField()

Json:

$.getJSON("?customer-reservation=" + $(this).attr("data-id"), function (data) {
            console.log(data);
            reservationFill(data);
        });

And finally the view:

    if request.args.get("customer-reservation"):
        data = rzv.objects(restaurant=rest, customer=cdb.objects.get(id=request.args.get("customer-reservation"))).all()
        return data

What is the right way to query this situation. Do i have to use a filter ?

like image 962
cankemik Avatar asked Jan 14 '14 21:01

cankemik


People also ask

How do you query in MongoEngine?

The connect() function returns a MongoClient object. Using list_database_names() method available to this object, we can retrieve number of databases on the server. It is also possible to obtain list of collections in a database, using list_collection_names() method.

Should I use PyMongo or MongoEngine?

Both PyMongo and MongoEngine can be used to access data from a MongoDB database. However, they work in very different ways and offer different features. PyMongo is the MongoDB recommended library. It makes it easy to use MongoDB documents and maps directly to the familiar MongoDB Query Language.

What is document in MongoEngine?

MongoEngine defines a Document class. This is a base class whose inherited class is used to define structure and properties of collection of documents stored in MongoDB database. Each object of this subclass forms Document in Collection in database.


2 Answers

You should split the query:

customer = Customers.objects(id=request.args.get("customer-reservation")).get()
data = Reservations.objects(restaurant=rest, customer=customer).all()

Also you will need error handling for any customers that don't match.

like image 94
Ross Avatar answered Sep 19 '22 02:09

Ross


You can use in – value is in list (a list of values should be provided) in a single line:

data = Reservations.objects(restaurant=rest, 
               customer__in=Customers.objects.filter(id="your filter id")).all()
like image 33
ShaDow RiDeR Avatar answered Sep 21 '22 02:09

ShaDow RiDeR