.eq() and multi-reference fields

Hi,

The code below works fine when querying anything BUT multi-reference fields. Once I choose a multi-reference field, I get 0 in my console. Why??

import wixData from 'wix-data';

wixData.query("LawDatabase")
  .eq("tags","books") <-- in this specific case, 'tags' is a tags field type and it works great.
  .count()
  .then( (num) => {
 let numberOfItems = num;
    console.log(num)
    $w("#countStats").text = num.toString()
  } )
  ;

Thank you.

Use .include() to multi-reference. See the doc. Include has a limit 50.

And if you get lost by using “include” then take a look here…

https://russian-dima.wixsite.com/meinewebsite/how2-include

Hi there @russian-dima and @mvveiga !

So I’ve got this:

import wixData from 'wix-data';

wixData.query("LawDatabase")
  .include("lawtags")
  .find("books")
  .then( (num) => {
 let numberOfItems = num;
    console.log(num)
    $w("#countStats").text = num.toString()
  } )
  ;

I query ‘LawDatabase’ collection to find the value ‘books’ in the multi-reference field ‘lawtags’. Okay. Now how do I count the values (=the times ‘books’ appears) and send the total count to the console?

@gemats

i don’t know if this works, because i can’t reconstruct it right now, but try this…

import wixData from 'wix-data';

wixData.query("LawDatabase")
  .include("lawtags")
  .find("books")
  .count()
  .then( (num) => {
 let numberOfItems = num;
    console.log(num)
    $w("#countStats").text = num.toString()
  });

And it is also good to see what your console log shows you. Then we can also see the behaviour of your code. What it does and which results you get back. :wink:

@russian-dima did this myself already but I get this:

@gemats
Ok, wrong way xD

Perhaps you take one more time at the example and uses the console-window.
Perhaps you may use “.length” for your purposes.
https://russian-dima.wixsite.com/meinewebsite/how2-include

import wixData from 'wix-data';
 
$w.onReady(function () {   });
 
export function BTN_click(event) { start_Function()  }
 
function  start_Function() {
    wixData.query("Books").include("author").find()
    .then( (results) => {
        if(results.items.length > 0) {
        let books = results.items;
        let firstBook = books[0];
        let firstAuthor = firstBook.author;
        console.log(books)
        console.log(firstBook)
        let output1 = [], output2 = []
 
        for (var i = 1; i < results.items.length; i++) {
            output1.push(books[i].author.authorFirstname)
            output2.push(books[i].author.authorLastname)
        }
        console.log(output1, output2)
        $w('#TXToutput1').text=output1.toString()
        $w('#TXToutput2').text=output2.toString()
    } else {    }
  } )
}

@russian-dima I’m not keen in using a i+1 loop in order then to use .length to count such a simple thing… so many lines of code for a count??

Anyway, did this:

import wixData from 'wix-data';

wixData.query("LawDatabase")
    .include("lawtags")
     .find("books")
     .then( (results) => {
 let resultLength = results.length; 
    console.log(resultLength);
  } ) ;

console log sends no message, is blank.

EDIT1: If I remove ‘books’ from find(), console gives me 50. Have to find out what this is.
EDIT2: from the docs: "Each returned item can include up to 50 referenced items. "

Try .aggregate(). See the doc https://www.wix.com/corvid/reference/wix-data/wixdataaggregate. .find() has no parameter.
I guess you want to filter the query for “books” then use .eq() or .contains() for that.

My problem is that I can’t reach a point where to query and count the number of ‘books’ which is one of the many values of the ‘multi-reference’ field ‘lawtags’. The .aggregate() (like the include()) does not allow me to define the exact multi-reference value I’m looking for…

If the all referenced are “books”, use the reference.length. Reference filed is an array of ids from the referenced colelction.

No they’re not. How could they be? I’m giving an example:

We have a 5 field Collection.
First field: id
Second field: Movie name (text)
Third field: Movie length (text)
Fourth field: Movie budget (number)
Fifth field: Movie’s actors (multi-reference)

An example row of five fields:

1 | Titanic | 2h 30min | $5000000 | Leonardo DiCaprio - Kate Winslet - XXXX - YYYY

So what I want to code is: go to Fifth field, find value ‘Leonardo DiCaprio’ and count how many times ‘Leonardo DiCaprio’ appears in the collection, or in other words ‘move vertically’, meaning, in how many movies have this actor participated.

Please have in mind: I have another field, next to this one, with tags field type , where I have the exact same values for each row. My code works flawlessly there and it counts correctly all DiCaprios found in this column (Fifth) of my database collection…

1 - Leonardo should be listed only once on Titanic.
2 - I guess you can query the “Actors” filtering" by Leonardo then you get all movies Leonardo is actor.
So on the query movie, do another query on ‘actors’.
I missing something because you started with books and now you are using

Movies = Collection Movies
Column Actors = array of actors ID => multi - reference to actors
Length returns total actors in that movie (Leonardo, Kete)

Actors = Collection Actors
Column Movies = array of movies ID => multi reference to Movies
Length returns total movies that actor works (Titanic, …)

This is not what I’m looking for @mvveiga. I do not look for ‘total actors in that movie’. By using .length(), according to documentation I have no ‘room’ to specialize my search to a specific value of the multi-referenced field and I get all my referenced items up to number 50. At least this is what I understand. The same goes with include() or aggregate(). I can only point my finger to the field, and not any of its values (like in the code below).

So what I’m looking for is ‘total appearances of X actor’ in my database, in other words, total times this actor appears vertically in this multi-reference-valued column/field.

Which is something that I can get super-easily if I put the actors in a simple tags field type and then run the following code:

import wixData from 'wix-data';

wixData.query("myMovies")
  .eq("Actors","Leonardo") <-- in this case, 'Actors' is a tags field type and it works.
  .count()
  .then( (num) => {
 let numberOfItems = num;
    console.log(num)
    $w("#countStats").text = num.toString()
  } )
  ;

I got, sorry. Maybe queryReferenced() can help.

@gemats
Perhaps you should give @mvveiga some insight into the structure of the related databases. A little pic of some part of your DB could help to understand things better and faster.

P.S.: I am sure, you will find your solution, you are not a beginner in coding :grin:

@russian-dima I’d like to say that @mvveiga is very helpful and I honestly appreciate the time he/she invests in helping me. But I believe I have made it very clear regarding what I’m doing here. No fancy things. Just counting of how many times a specific value appears in a multi-referenced field in my collection, like for example, in how many movies an actor has played. Movies collection, Actors collection. Movies collection has a multi-reference field link to Actors’ names.

So a movie record/row has 15 actors in it. Another movie has 20 actors in it, and some of them overlap, are the same. So I want to code: how many movies has Actor A played in? Actor B?

Now, I can do this , if inside Movies collection, I add a tags field type field and I manually repeat the multi-referenced values. But this is not correct and this is not good coding. I’d like to do these calculations (find X and then count how many times X appears) with the multi-reference field directly . But I can’t ‘talk’ to a multi-reference field with the .eq function. Nothing happens.

If I use include() or aggregate(), I’m obliged by the API to also use find(), but find() does not get any parameters apparently, so I can’t say
-include(“Actors”)
-find (“Leonardo”)
-count()…

So I’m stuck.