Java code - how to return multiple field / values

I am an appery beginner / newbie and need a push into the right direction.
I have a query service and want only display records (Appery DB collection) when 2 conditions are true. I am using this code:
return '{"Catno":"' + value + '", "Subcatno":"' + value + '"}';

When I only use one return statement i.e. Catno than it works, but the result is not complete.

Could someone help me with this code? I appreciate it.
1 person has
this question
+1
Reply
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned kidding, amused, unsure, silly sad, anxious, confused, frustrated

  • Hi, i don not understand yet how this would fit in my app. Do I have to put the "and" part in front of my code?
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned kidding, amused, unsure, silly sad, anxious, confused, frustrated

  • Yes, please add JS below on the request parameter "where":

    return '{"$and": [{"priority": "Low"},{"taskName": "Create an app"}]}';
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned kidding, amused, unsure, silly sad, anxious, confused, frustrated

  • I’m thankful
    Thanks. Will try that and let you know.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned kidding, amused, unsure, silly sad, anxious, confused, frustrated

  • Hi unfortunately is not working, because the lookup needs to be done in another array.
    I want only list records from a mongo collection where catno = 2 and the same value (catno =2) has an apperance in a localstorage (of type model).

    I have tried several combi;s of below but all did not work. Probably wrong syntax again.

    return '{"$setIntersection":[mselectedcat,mmasterALL]{"Catno":"' + value + '"}}';

    What am I doing wrong?

    I appreciate your help.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned kidding, amused, unsure, silly sad, anxious, confused, frustrated

  • I'll try to help you with this as well. Here is what I use to return results in one of my many cases where I need this:

    //filtering return with a range criteria and basic inquery
    var whereObject = {floor_ptr: {"$inQuery": {_id: value } }, "status_value": 1 };
    return JSON.stringify(whereObject);

    I am running this on my collection of rooms.

    In this case, floor_ptr is a pointer value in the database to another collection, floors. I want to use a floor's ID to show only rooms that are on that floor.

    Also, I want only empty rooms. In my setup each room record has a status, stored in the field status_value, and empty rooms are numerically codes as 1.

    Here is a pic of where in my mapping this code is located:


    I am mapping that from a storage variable that has an id (string), so in that code, the variable "value" is the actual ID value that I am filtering on. 1 is just a numerical value.

    The result of this will give me an array of all rooms on that selected floor that are empty.

    I am not sure what you mean in your last post that catno=2 needs to have an appearance in local storage.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned kidding, amused, unsure, silly sad, anxious, confused, frustrated

  • Hi,

    Thanks. What you do with your rooms is the same (sort of) I want to achieve with my cats and subcats.

    I have two collections
    - Subcat (Descr, Catno, Subcatno)



    - Masters (Catno, SUbcatno, custno)



    I only want to list records from the Subcat collection (catno=1) when there is a Catno=1 record in the Masters collection. Cat=1 only needs to have an appearance in the collection.

    The result would be only records with subcats 2, 3 and 5 from subcatlist.

    The only JS is have in the where before send service => return '{"Catno":"' + value + '"}'; but this lists all



    Hope this is a little bit more clear.

    Thanks for your help!
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned kidding, amused, unsure, silly sad, anxious, confused, frustrated

  • console.log("value = " + value);
    var whereObject = {"Catno": value };
    return JSON.stringify(whereObject);

    That is the proper syntax. Assuming you are mapping over the value of 1, this will return all records with a Catno of 1. Is this what you are looking for?

    If not, I suggest a redesign of your collection structure. I dont fully understand what you are trying to do, but I would suggest creating a 'child-parent relationship' with master as the parent and the subcat collection having a field to master that is a pointer. Then, every time the master record is selected or becomes relevant, map it to a storage named selected_master and use its ID to filter for a list of subcats with that pointer value to the parent master record.

    I included the console.log segment since it is very handy when testing a new setup.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned kidding, amused, unsure, silly sad, anxious, confused, frustrated

  • Hi thanks for your reply. I have made a typo. I want to.list all records from subcatno WHERE catno =1 AND the subcatno of that record exist in the master collection.
    My apologies for the typo. I will look into your code and try it out. Let you know.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned kidding, amused, unsure, silly sad, anxious, confused, frustrated

  • As a general rule of thumb, if you ever want to filter a collection by a piece of data, make sure that data can be found in the same collection, and the situation you are in is why. You could possibly trigger a separate service that looks at the master collection for a specific subcatno, and if it finds one, have it return 'true', but if not, 'false'. You could do that, but I strongly suggest restructuring your collections with pointers, with the subcat collection having a pointer value to a master record. That way all of the data for that master record will be "at hand" every time you look as the subcatno.

    For example, with pointers I can look at a single room purchase record from the Stays collection and tell you ALL of that related customer's info, the info for that room, for that floor and for that hotel, including ID's for each value that I could use later. Again, the best part of this is that all I had to do was look at a single record in one table to get all related records in other collections. One service request, one response with multiple collection's info records.

    I suggest that based off the little I know about what you are trying to do here. Looking at your pics of the database inclines me to suggest that as well. If you think you need to keep it the same way, then could you send me some screenshots of your storage model along with an explanation of what info is related to what and through what fields please?
    • The pointer could work. I use subcat as the parent and master as the child. The catno and subcatno are in both collections and could be linked.

      I found this op the apperyio website:

      *****************************
      To read multiple children for a parent, use the following where parameter, where columnName is the name of the column with type=pointer and _id is the referenced object of the todo database:

      {"columnName": {"$inQuery" : { "_id": "54da2471e4b0e533414b6115"}}}

      The pointer value can be set using a POST command and by passing two parameters – collName and _id of referenced object:

      cURL:
      curl -X POST
      -H "X-Appery-Database-Id: 542416a1e4b0b7fdcc764eb0"
      -H "Content-Type: application/json"
      -d '{"owner": {"collName":"_users", "_id":"5540ed6ae4b020ea2fabaf44"}}'
      https://api.appery.io/rest/1/db/colle...

      Example above will create a new entry in the students collection with owner field type of pointer.

      To retrieve the referenced object value (not a pointer value) use the include parameter.
      **************************************
      How does this fit in my app? I have watched the instructionvideo and documents but those did not point me into the proper direction.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned kidding, amused, unsure, silly sad, anxious, confused, frustrated

  • Unfortunately not successful.
    Try to give more details about what I want to achieve.

    I have two collections: SUBCAT and MASTER
    Subcat holds all combinations of categories and subcategories (just for listing purposes).
    Master holds only the active categories and subcategories. I call these "active subcategories".

    The app shows a list of all 5 categories. When a category is selected the app shows a new screen with all subcategories from the subcat collection.

    What I want to achieve is that when a category has been selected only the subcategories from Subcat are listed when there is a record in MASTER with that subcategory.

    Hopefully I am clear. I just don't know how to list from one collection and do a check for existence in another collection. Then I have to give this a spot in the Apperyio app.
    Do I have to activate a query service of the master collection too and how do I request that service when I have the return statement in the Subcatservice?

    I am very lost and can't find a example or instructions anyware.

    Hope you can help me with further details.

    Thanks for your time and help!
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned kidding, amused, unsure, silly sad, anxious, confused, frustrated

  • Unfortunately I cannot seem to understand why you have a setup like this. I am not saying it is wrong, but I cannot identify any points of improvement in the structure since I just don't understand it or its purpose, so i'll leave that decision to you.

    If a restructure is out of the question or determined unnecessary, then your options for trial are limited. If you are trying to do this for each item in a list then I cannot help you as I asked the same thing and did not find a solution.

    https://getsatisfaction.com/apperyio/...

    If you are wanting to do this with one record that is then triggering a service onto another collection, (like after selecting an item from a list) then I suggest storing the value you want from the first record and using that (along with any other information you need to use) to filter the master record. If the response has something in it, write code to return true. If it responds with nothing, return false. This code of true or false would have to be done on the success event of the service (datasource) for that page. I would test around a little with an if formula while using console.log to see what the syntax is before you try this for real.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned kidding, amused, unsure, silly sad, anxious, confused, frustrated

  • Our messages seems to cross each other. I would like this to do with pointers but don't know how to do that.

    I have a very simple DB design.

    !! in case you wonder why the master collection ...
    the master collection can have more than 1 cat,subcat for a custno. I have these for testing (cat: subcat: custno): "1":"2":"1" and "1":"3":"1" and 1:4:1

    In SQL in know how to work with joins, but this (MongoDB / pointers) is new for me.

  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned kidding, amused, unsure, silly sad, anxious, confused, frustrated

  • I too came from SQL (actually MySQL). Watch this video and I will get back to you very soon with a longer response:

    https://www.youtube.com/watch?v=RGGxT...
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned kidding, amused, unsure, silly sad, anxious, confused, frustrated

  • Ok, so what you have found are the basics of utilizing the features of pointers. include is extremely helpful, and is pretty much automated if a service is created on a collection AFTER it has pointer values set up.

    ID's are very important here.Your Masters collection would have a field (type: pointer to customers), and a record in the Maters collection for example, would have a value in that field that equals the id of the customer record in the Customers collection. I believe that is what customer no is illustrating in your drawing.

    As for cat ans subcat, do those have to be numerical values or can they be ID strings to which you can set up pointers?
    • view 1 more comment
    • I'm afraid I still do not understand the logic behind your structure or what is necessary. I've never ran across a need to check two records in two different collections to determine what I should do or display with one of the records. If this is an inventory thing, I suggest a log of 'movement', with each record pointing to a type of product. When looking at each product, a sum is done on the log with a filter for that product.

      The reason why I highly recommended pointers is because you most likely need them if you are making a database of several collections. AS for which one point to which, the 'many' side should point to the 'one' . For example, in a hotel setup (I don't actually use Appery for a hotel BTW), one floor has many rooms, so each room would have a pointer to the floor.

      I am sorry I cannot figure it all out for you but I spent the last 30 minutes trying to do so and I think it just comes down to you knowing your the needs of your situation better.
    • Hi Jack, Thanks for you reply again.
      Its not that I am not willing to work with pointers, but can not see how it will solve my issue OR better I don't understand....

      The subcat collection contains a description field too. So three fields Catno, Subcatno and Descr.

      The master collection only contains Catno, Subcatno and Custno. This collection contains active Customer, categories and subcategories combinations.

      The app now show all subcategories but I want to show only those subcategories that have a record in the master collection. Note: the master collection does not have to contain all combinations of category and subcategory.

      I just want to list records from one collection when there is a record of the same key (combination of category and subcategory) in the other collection.

      This is the best explanation I can give .....

      I already have all the subcategories in localstorage. I could try to write code to lookup, record by record if there is a combi(Cat, Subcat) in the master collection AND if so write to localstorage.
      But I truly believe there must be a more efficient way to do this. Now for me to find out how...

      Pointers? I don't see how... was it just simple like "select from subcat collection where catno, subcatno in masters collection" :o)

      MongoDB has a intersection parameter. Reading the doc it seems that this can do the trick. Now to find out how and if I need a pointer for this ....

      Thanks! I appreciate you help.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned kidding, amused, unsure, silly sad, anxious, confused, frustrated

  • Instead of showing a list based off the subcat collection, base it off the masters collection, with the subcatno field being a pointer to the subcat collection. Secondly, if necessary, add a field to masters with the type of boolean that is true if active, false if not. That might not be necessary, so if it is not and you would prefer to shot all subcats if they are in the masters table, then disregard that.

    I would suggest creating a new masters collection, or at least deleting all your database services for that collection then recreating them once you get the collection created. (this will save you from manually having to define a lot of parameters). I may be mistaken, but if a response includes a pointer, an include query is automatically created. Be sure to check for that as it is critical.

    When you make your list, populate it with a service on the masters table. In the success event, in the mapping, under the service response you should see that subcatno (or whatever your pointer to subcat collection is named) will have the ability to expand. In there you will find all your subcat fields (because of that include query in the service setup). Map those to your list.

    To make sure this is what you will want, here is another hotel example.

    Lets say I want an emergency head-count page (in case of fire evacuation).My boss has tasked me with account for all the guests on the second floor. I am standing in the parking lot with 100 guests, all from the 2nd floor. I can care less what room they are from, I just need a list of guest names from that floor. In the emergency head count page, I select the floor I am assigned too. It take me to another view, and all I see is a list with a hundred guest names, all of which happen to be staying in rooms on the second floor.

    Behind the scenes, this list is not pulling from my guest/customers collection, it is pulling from the purchase collection, which includes a pointer value to customers, pointer, to rooms, a pointer to floors(which is redundant since the Rooms collection has a pointer to floors but this field is needed here for where parameter filtering), a start date and an end date. There is a where parameter filtering the results for second floor only, and the response is being mapped from the included customers pointer.

    In summary, this allows me to give you a list of information from the customers collection yet filtered by information that resides on another collection.

    If I am confusing you let me know, but I am pretty sure this is what you need.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited indifferent, undecided, unconcerned kidding, amused, unsure, silly sad, anxious, confused, frustrated