how to store indexes in pouchdb

79 Views Asked by At

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?

2

There are 2 best solutions below

0
Black Coder On

in PounchDb when created index using Mongo Queries like

db.createIndex()

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

0
A_Safarzadeh On

Ok so if we want to go a bit deeper, Both Mango queries and Map/Reduce queries in PouchDB create indexes that are stored in the database and updated with every change in the database.

When you use db.createIndex(), PouchDB creates an index and stores it in the database. This index is not recreated every time you call db.createIndex(). Instead, PouchDB checks if the index already exists and if it does, it simply uses the existing index. If the index does not exist, it creates a new one.

Similarly, when you use Map/Reduce queries, the index is stored in the database and updated with every change. The index is created when you save the design document using pouch.put(ddoc). When you call db.query('my_index/by_name'), PouchDB uses the existing index. If the index does not exist, it throws an error.

So, to answer your question, both Mango queries and Map/Reduce queries maintain stored indexes. The choice between the two depends on your specific use case. Mango queries are simpler and easier to use but they are less flexible than Map/Reduce queries.