Search code examples
arraysparse-platformparse-cloud-codenosql

Parse.com: query on an array field not working with a big number of values


I use Parse.com Core and Cloud Code to store data and perform some operations for a mobile app. I have an issue with a query on an array field that is sometimes not returning anything even if I am sure it should.

I store a large amount of phone numbers in an array field to keep track of user's matching contacts. This field is called phoneContacts and look like this (with numbers only, this is just as an example):

["+33W30VXXX0V","+33W30VXX843","+33W30VZVZVZ","+33W34W3X0Y4","+33W34W386Y0", ...]

I have a function in Cloud Code that is supposed to get matching rows for a given phone number. Here is my query:

var phoneNumber = request.params.phoneNumber;
var queryPhone = new Parse.Query('UserData');
queryPhone.equalTo('phoneContacts', phoneNumber); // phoneNumber is passed as a string param, i.e. "+33W30VXX843"

queryPhone.include('user');

var usersToNotify = [];
return queryPhone.each(function(userData) {
  var user = userData.get('user');
  usersToNotify.push(user.get('username'));
})
.then(function() {
  return usersToNotify;
});

I tested my query with an array of 2 or 3 phone numbers and it works well and returns the expected rows. But then I tried with a user having around 300 phone numbers in that phoneContacts field and even if I query a value that is present (appear with a filter in Parse Data Browser), nothing is returned. To be sure I even took a phone number existing in 2 rows: one with few values and one with many, and only the row with a few values got returned.

I've read carefully the Parse documentation and especially about queries and field limits, but it doesn't seem to have a restriction on the number of values for an array field, and nothing says that query might not work with a lot of values.

Anybody can point me in the right direction? Should I design my Parse Classes differently to avoid having so many values in an array field? Or is there something wrong with the query?


Solution

  • You need to be using a PFRelation or some sort of intermediate table. You should not use an array to store 300 phone numbers, your queries will get really slow.

    PFRelations:

    https://parse.com/docs/osx/api/Classes/PFRelation.html http://blog.parse.com/learn/engineering/new-many-to-many/