Laamiri Oussema
Laamiri Oussema

Reputation: 527

Create collections in cloudant

I'm trying to build an ionic application which retrieves data from Cloudant using pouchdb. Cloudant allows creating only databases and documents. How can I create some collections in Cloudant?

Upvotes: 0

Views: 451

Answers (2)

ShwetaJ
ShwetaJ

Reputation: 519

Long post, but hope that helps.

I would like to explain this with a RDBMS analogy.

In any RDBMS, a new database would mean a different connection with different set of credentials. A collection would mean the set of tables in that particular database. A record would mean a row in a table.

Similarly, you can look at a single Cloudant service instance as a database(RDBMS terminology). A collection would be a "database" in that service instance in Cloudant's terminology. A document would correpond to a single row.

Hence, Cloudant has no concept of collection as such. If you need to store your related documents in a separate collection you must do it with multiple databases within the same service instance.

If you want to use only a single database, you could create a field like "record_index" to differentiate between the different documents. While querying these documents, you could use an index. For. e.g. I have a student database. But I do not want to store the records for Arts, Commerce, Science branches in different databases. I will add a field "record_type": "arts", etc. in the records. Create an index,

{ selector: {record_type: "arts"}}

Before doing any operation on the arts records, you can use this index and query the documents. In this way, you will be able to logically group your documents.

Upvotes: 0

ptitzler
ptitzler

Reputation: 923

Two part answer:

A set of documents that meet certain criteria can be considered a collection in Cloudant/CouchDB. You can create views to fetch those documents. Such a view might check for the existence of a property in a document ("all documents with a property named type"), the value of a property ("all documents with a property named type having the value of book") or any other condition that makes sense for your application and return the appropriate documents.

You basically have to follow a three step process:

  • determine how you can identify documents in your database that you consider to be part of the collection
  • create a view based on your findings in the previous step
  • query the view to retrieve those documents

Above documentation link provides more details.

Properties in your document can represent collections as well, as in the following example, which defines a simple array of strings.

{
  "mycollectionname": [
      "element1",
      "element2",
      ...
  ]
}

How you implement collections really depends on your use-case scenario.

Upvotes: 1

Related Questions