我有一个关于 JSONStore searchFields 的问题。
如果我number用作 searchFields 键并尝试通过作为查询的WL.JSONStore.find方法查找数据0,它将命中所有数据(未过滤)。
使用上述integer情况可以正常工作。
number和有什么区别integer?
我有一个关于 JSONStore searchFields 的问题。
如果我number用作 searchFields 键并尝试通过作为查询的WL.JSONStore.find方法查找数据0,它将命中所有数据(未过滤)。
使用上述integer情况可以正常工作。
number和有什么区别integer?
JSONStore 使用 SQLite 来持久化数据,您可以在此处阅读有关 SQLite 数据类型的信息。简短的回答是number将数据存储为,REAL而integer将数据存储为INTEGER.
如果您创建一个名为的集合nums,其中一个名为的 searchFieldnum类型为number
var nums = WL.JSONStore.initCollection('nums', {num: 'number'}, {});
并添加一些数据:
var len = 5;
while (len--) {
nums.add({num: len});
}
然后find使用查询调用:{num: 0}
nums.find({num: 0}, {onSuccess: function (res) {
console.log(JSON.stringify(res));
}})
你应该回来:
[{"_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}}]
请注意,您取回了所有存储的文档(num = 4、3、2、1、0)。
如果您查看 .sqlite 文件:
$ cd ~/Library/Application Support/iPhone Simulator/6.1/Applications/[id]/Documents
$ sqlite3 jsonstore.sqlite
(android文件应该在下/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);
注意 num 的数据类型是REAL。
运行与 find 函数中使用的查询相同的查询:
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
注意4存储为4.0JSONStore 的查询始终使用LIKE,任何带有 a 的 num0都将匹配查询。
如果您integer改用:
var nums = WL.JSONStore.initCollection('nums', {num: 'integer'}, {});
查找回报:
[{"_id":5,"json":{"num":0}}]
这schema表明 num 具有INTEGER数据类型:
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
为简洁起见,我跳过了一些onSuccess和所有的onFailure回调。
JSON数字和整数之间的实际区别是
defining {age: 'number'} indexes 1 as 1.0,
while defining{age: 'integer'} indexes 1 as 1.
希望你能理解