JSONStore difference between 'number' and 'integer' in searchFields

后端 未结 2 1523
孤城傲影
孤城傲影 2021-01-23 04:05

I have a question about JSONStore searchFields.

If I use number as the searchFields key and try to find data by WL.JSONStore.find method with <

相关标签:
2条回答
  • 2021-01-23 04:39

    JSONStore uses SQLite to persist data, you can read about SQLite Data Types here. The short answer is number will store data as REAL while integer will store data as INTEGER.

    If you create a collection called nums with one searchField called num of type number

    var nums = WL.JSONStore.initCollection('nums', {num: 'number'}, {});
    

    and add some data:

    var len = 5;
    while (len--) {
        nums.add({num: len});
    }
    

    then call find with the query: {num: 0}

    nums.find({num: 0}, {onSuccess: function (res) {
        console.log(JSON.stringify(res));
    }})
    

    you should get back:

    [{"_id":1,"json":{"num":4}},{"_id":2,"json":{"num":3}},{"_id":3,"json":{"num":2}},{"_id":4,"json":{"num":1}},{"_id":5,"json":{"num":0}}]
    

    Notice that you got back all the documents you stored (num = 4, 3, 2, 1, 0).

    If you look at the .sqlite file:

    $ cd ~/Library/Application Support/iPhone Simulator/6.1/Applications/[id]/Documents
    $ sqlite3 jsonstore.sqlite
    

    (The android file should be under /data/data/com.[app-name]/databases/)

    sqlite> .schema
    CREATE TABLE nums ( _id INTEGER primary key autoincrement,  'num' REAL, json BLOB, _dirty REAL default 0, _deleted INTEGER default 0, _operation TEXT);
    

    Notice the data type for num is REAL.

    Running a query the same query used in the find function:

    sqlite> SELECT * FROM nums WHERE num LIKE '%0%';
    1|4.0|{"num":4}|1363326259.80431|0|add
    2|3.0|{"num":3}|1363326259.80748|0|add
    3|2.0|{"num":2}|1363326259.81|0|add
    4|1.0|{"num":1}|1363326259.81289|0|add
    5|0.0|{"num":0}|1363326259.81519|0|add
    

    Notice 4 is stored as 4.0 and JSONStore's queries always use LIKE, any num with a 0 will match the query.

    If you use integer instead:

    var nums = WL.JSONStore.initCollection('nums', {num: 'integer'}, {});
    

    Find returns:

    [{"_id":5,"json":{"num":0}}]
    

    The schema shows that num has an INTEGER data type:

    sqlite> .schema
    CREATE TABLE nums ( _id INTEGER primary key autoincrement,  'num' INTEGER, json BLOB, _dirty REAL default 0, _deleted INTEGER default 0, _operation TEXT);
    
    sqlite> SELECT * FROM nums WHERE num LIKE '%0%';
    5|0|{"num":0}|1363326923.44466|0|add
    

    I skipped some of the onSuccess and all the onFailure callbacks for brevity.

    0 讨论(0)
  • 2021-01-23 04:53

    The actual difference between a JSON number and integer is

    defining {age: 'number'} indexes 1 as 1.0,
    while defining{age: 'integer'} indexes 1 as 1.
    

    Hope you understand

    0 讨论(0)
提交回复
热议问题