Query on an array of objects

#1

I am looking to query on an array of objects using the Parse Javascript SDK,

Data sample :

{ objectId: 'xyxyxyxyx',
  tags: [
    {"name" : "love", "auto" : true},
    {"name" : "war", "auto" : false}
  ]
}

Queries I tried unsuccessfully:

var query = new Parse.Query('Tests');
query.containedIn('tags.name', ['love', 'war']);
query.find()
var query = new Parse.Query('Tests');
query.containedIn('tags', [{'name':'love'},{'name','war'}]);
query.find()

Do you have an idea how to do this?

#2

Not sure why your code doesn’t work, but below is a working example.

And a note on what this code is and how it works: this is a test written to be run by jasmine. An example of how this is setup can be found in the Parse-SDK-JS codebase’s integration tests which are an excellent way to test out stuff like this in isolation.

describe('test', function () {
  it('should find objects in an array with containerIn ', async function () {
    await new Parse.Object('Test').save({
      tags: [
        { name: 'love', auto: true },
        { name: 'war', auto: false },
      ],
    });

    await new Parse.Object('Test').save({
      tags: [
        { name: 'fred', auto: true },
        { name: 'alice', auto: false },
      ],
    });

    await new Parse.Object('Test').save({
      tags: [
        { name: 'love', auto: true },
        { name: 'alice', auto: false },
      ],
    });

    const result = await new Parse.Query('Test')
      .containedIn('tags.name', ['love', 'war'])
      .find();

    expect(result.length).toBe(2);
  });
});
1 Like