【发布时间】:2018-04-12 14:17:00
【问题描述】:
我在 React Native v0.46.2 项目中使用本地 PouchDB 实例。我创建了一个数据库,向其中添加记录,执行 Mango 查询(搜索具有特定事件 ID 的文档),查询按预期工作。然后我修改了一条记录,执行和之前一样的查询,结果不正确。似乎只返回了我刚刚修改的文档,而没有返回任何其他应该返回的文档。如果我之后打印所有文档,它会显示所有文档,因为它们应该包括新的修改。
我已关闭索引,查询工作正常。但我确实需要索引才能对大量数据执行快速搜索。
为什么我的查询在修改文档后停止正常工作?
我创建了下面的示例数据代码来演示该问题。我按此顺序执行以下操作:
- 创建索引
- 带有空白查询的索引数据库
- 将 bulkdocs 添加到 db
- 执行查询
- 修改文档
- 再次执行查询。
`
testFunction() {
//Dummy data
let mockData = [{
'_id': '1',
'event_id': '136471',
},
{
'_id': '2',
'event_id': '136471',
},
{
'_id': '3',
'event_id': '136471',
},
{
'_id': '4',
'event_id': '136472',
}];
//Create DB
this.testDB = new PouchDB('DBTest');
let thisTestDB = this.testDB;
this.testDB.createIndex({
index: {
fields: ['event_id'],
ddoc: 'testTicketsDesignDoc',
},
})
.then(
(result) => {
console.log('Indexing for Mango complete');
//Index mango with initial blank query
thisTestDB.find({
selector: {},
use_index: 'testTicketsDesignDoc',
limit: 0,
})
.then(
(result) => {
console.log('Indexing with blank Mango query complete');
//Add docs to database
thisTestDB.bulkDocs(mockData)
.then(
(result) => {
console.log('Bulkdocs successfully added to database.');
//Perform 1st query before modifying docs
thisTestDB.find({
selector: {
'event_id': '136471',
},
use_index: 'testTicketsDesignDoc',
})
.then(
(searchResult) => {
console.log('1st Mango query complete');
console.log(searchResult);
//Now modify a doc
thisTestDB.get('1')
.then(
(doc) => {
//Make any modifications to doc here
thisTestDB.put(doc)
.then (
(putResult) => {
console.log(`Modifying doc successful: ${JSON.stringify(putResult)}`);
//Perform second query after modifying docs
thisTestDB.find({
selector: {
'event_id': '136471',
},
use_index: 'testTicketsDesignDoc',
})
.then(
(searchResult) => {
console.log('2nd Mango query complete');
console.log(searchResult);
}
);
}
)
.catch(
(error) => {
console.log(`Error modifying doc: ${error}`);
}
);
}
)
.catch(
(error) => {
console.log(`Error modifying doc: ${error}`);
}
);
}
)
.catch(
(error) => {
console.log(`Error performing first query: ${error.message}`);
}
);
}
)
.catch(
(error) => {
console.log(`Error adding bulk docs: ${error}`);
}
);
}
)
.catch(
(error) => {
console.log(`Error performing initial indexing query: ${error}`);
}
);
}
)
.catch(
(err) => {
console.log(`Error - ${JSON.stringify(err)}`);
}
);
}
另外,我没有修改文档,而是尝试删除文档并压缩,然后放入文档的新副本。我在搜索索引时仍然遇到同样的问题。
【问题讨论】:
标签: pouchdb