简体   繁体   中英

Retrieving list of specific objects from Parse.com collection by objectId

I'm building a food delivery app using AngularJS and using Parse.com as my BaaS.

After a user constructs their orders, they pay for the order and the order is then saved to Parse.com.

Since the maximum array size on Parse.com for an array column is 128KB, I kept the representation of the order as simple as possible. I have an array column for items . To identify each element in the items array, I only keep the objectId and the quantity .

Another concern of mine is to minimize Parse.com queries given that bandwidth is metered.

- for each Order
    - for each Order Item
        - for each Menu Item
            - if Order Item ID == Menu Item ID, copy Menu Item data.

I'm hard pressed to believe there is not a simpler way to do it. But most of the documentation I've seen doesn't say much about retrieving a subset of a collection by objectIds.

I believe this is the relevant page of documentation:

https://parse.com/docs/js/api/classes/Parse.Query.html

Does anyone have any insight as to what would be the best approach to solving this problem without breaking this up into multiple Parse.com queries?

EDIT: If you need to see the code, sure. It works. I just know from an algorithmic standpoint, this is totally inefficient.

I think you may be overlooking a much better way to structure your data.

First, to clarify a misleading statement, 128KB is the maximum size of any class object in Parse, not the maximum size of an array. This is everything except for Parse Files. Furthermore, a 128KB array would be massive and is well beyond your storage needs based on the description.

In terms of structuring your data, consider creating a class called OrderItem. Within this class you have a pointer to a MenuItem and number for the quantity. Then within your Order class, create an array of pointers to OrderItems.

The classes could be something like this:

  • Order
    • default metadata - objectId, createdAt, updatedAt, etc
    • user: pointer to user
    • itemArray: array of pointers to OrderItem
    • whatever other data you want
  • OrderItem
    • default metadata
    • menuItem: pointer to MenuItem
    • quantity: number
  • MenuItem
    • default metadata
    • food info, restaurant info, etc

You should use pointers as often as possible to avoid redundancies in your database and keep your design as consistent as possible. I'm unsure what the goal is of copying the Menu Item data in your sample code, so I will give a different example.

Using this design, you could query for all of a user's orders, including the data related to the order, by using the following:

var query = new Parse.Query("Order");
query.equalTo("user", request.user);

// Include the pointer data (dot notation used for pointer to pointer)
query.include("itemArray.menuItem");

query.find({ 
    success: function(results) {
        console.log(results);
        // Do something with results
    },
    error: function() {
        response.error("query failed");
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM