我在React Native v0.46.2项目中使用本地PouchDB实例。我创建一个数据库,向其添加记录,执行Mango查询(搜索具有特定事件ID的文档),并且查询按预期工作。然后我修改一条记录,执行与以前相同的查询,结果不正确。它似乎只返回我刚修改的文档,但没有其他文档应该返回。如果我之后打印所有文档,它会显示所有文档,因为它们应包括新修改。
我关闭了索引,查询工作正常。但我确实需要索引才能快速搜索大量数据。
修改文档后,为什么我的查询无法正常工作?
我在下面创建了示例数据代码以演示此问题。我按此顺序执行以下操作:
`
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)}`);
}
);
}
此外,我还尝试删除文档和压缩,然后将文档的新副本放入。而不是修改文档。我仍然遇到搜索索引的相同问题。
答案 0 :(得分:1)
我尝试通过创建index.html
文件和包含JavaScript代码的so.js
文件(稍加修改)来重现您的问题条件,我发现第一和第二个查询都有效正如预期的那样没有任何问题,控制台日志如下所示。请注意,第二个查询返回一个带有_rev: "2-d36..."
的文档,该文档是修改后的文档:
只需仔细检查,这是我的index.html
文件:
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<title>Pouch Debug</title>
</head>
<body>
<script src="//cdn.jsdelivr.net/npm/pouchdb@6.4.3/dist/pouchdb.min.js"></script>
<script src='//unpkg.com/pouchdb@6.4.3/dist/pouchdb.find.js'></script>
<script src='./so.js'></script>
</body>
</html>
我通过Python 3.5在我的index.html
文件目录中运行我的HTTP服务器,如下所示:
$ python3.5 -m http.server
Serving HTTP on 0.0.0.0 port 8000 ...
这是我的so.js
文件,在我的index.html
文件中使用。我对您的代码做了很少的更改:
testFunction();
function 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 = new PouchDB('DBTest') /*this.testDB;*/
thisTestDB.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)}`);
}
);
}