I am new to pouchdb and I want to create a few indexes for searching on secondary (non _id) fields.
I have seen the Mango & Map/reduce queries from the documentation, but I cannot understand whether the indexes created with each one of these query techniques are stored in the database and maintained (updated with every db change).
It seems to me that through the Mango queries, i.e. createIndex() and query() the index is created every time this is called, e.g.
db.createIndex({
index: {
fields: ['age', 'name'],
ddoc: "my-index-design-doc"
}
}).then(function () {
return db.find({
selector: {
name: 'mario',
age: {$gt: 21},
},
use_index: 'my-index-design-doc'
});
});
On the other hand, I think -but am not sure- that the map/reduce queries are stored to the database and can be later used
// document that tells PouchDB/CouchDB
// to build up an index on doc.name
var ddoc = {
_id: '_design/my_index',
views: {
by_name: {
map: function (doc) { emit(doc.name); }.toString()
}
}
};
// save it
pouch.put(ddoc).then(function () {
// success!
}).catch(function (err) {
// some error (maybe a 409, because it already exists?)
});
db.query('my_index/by_name').then(function (res) {
// got the query results
}).catch(function (err) {
// some error
});
Am I right? Which of the above does maintain the stored indexes?
in PounchDb when created index using Mongo Queries like
this is not stored persistently in database but instead its created as on the fly during the execution time and that means the index needs to be created again and again everytime when you run the query and using map/reduce queries and design documents the index will be stored persistently in database and when you create design and documentation with a map function then it will generate permanent index allowing for reuse feature also