Realm.js查询字符串列表属性

时间:2018-05-23 10:31:37

标签: javascript realm

目标

过滤字符串列表属性

代码示例

const BookrackSchema = {
    name: 'Bookrack',
    properties: {
        book_names:'string[]',
    }
};

Bookrack1.book_names=['A','B','C'];
Bookrack2.book_names=['B','C','D'];
Bookrack3.book_names=['C','D','E'];

a:realm.objects('Bookrack').filtered(`ANY book_names = $0 `,'B');
b:realm.objects('Bookrack').filtered(`book_names CONTAINS  $0 `,'B');  

预期结果

获得Bookrack1和Bookrack2

实际结果

aResult:The keypath following 'ANY' or 'SOME' must contain a list

bResult:Object type 'Table' not supported

1 个答案:

答案 0 :(得分:0)

目前,Realm JS仅支持查询对象列表,但我们计划添加对查询基本类型列表的支持。在那之前,你可以使用类似的东西:

const Realm = require('realm');

const BookrackSchema = {
    name: 'Bookrack',
    properties: {
        book_names:'string[]',
    }
};

let realm = new Realm({schema: [BookrackSchema]});

realm.write(() => {
    realm.create('Bookrack', { book_names: ['A','B','C']});
    realm.create('Bookrack', { book_names: ['B','C','D']});
    realm.create('Bookrack', { book_names: ['C','D','E']});
});

let objs = realm.objects('Bookrack');
let Bs = objs.filter(obj => { return obj.book_names.reduce((acc, cur)  => { return acc || cur === 'B' }, false)});