1

I'm working on Hyperledger fabric. I need a particular value from array not a full document in CouchDB.

Example

{
  "f_id": "1",
  "History": [
    {
      "amount": "1",
      "contactNo": "-",
      "email": "i2@mail.com"
    },
    {
      "amount": "5",
      "contactNo": "-",
      "email": "i@gmail.com",

    }
  ],
  "size": "12"
} 

I want only an email :"i2@mail.com" Object on history array, not a full History array.

mango Query:

{
   "selector": {
      "History": {
         "$elemMatch": {
            "email": "i2@mail.com"
         }
      }
   }
}

Output:

{
      "f_id": "1",
      "History": [
        {
          "amount": "1",
          "contactNo": "-",
          "email": "i2@mail.com"
        },
        {
          "amount": "5",
          "contactNo": "-",
          "email": "i@gmail.com",

        }
      ],
      "size": "12"
    } 

Full History array But needs only the first object of history array.

Can anyone guide me?

Thanks.

Isha Padalia
  • 877
  • 7
  • 24

1 Answers1

1

I think it's not possible, because rich queries are for retrieving complete records (key-value pairs) according to given selector.

You may want to reconsider your design. For example if you want to hold an history and query from there, this approach may work out:

  1. GetState of your special key my_record.
  2. If key exists:

    • PutState new value with key my_record.
    • Enrich old value with additional attributes: {"DocType": "my_history", "time": "789546"}. With the help of these new attributes, it will be possible create indexes and search via querying.
    • PutState enriched old value with a new key my_record_<uniqueId>
  3. If key doesn't exists, just put your value with key my_record without any new attributes.

With this approach my_record key will always hold latest value. You can query history with any attributes with/out pagination by using indexes (or not, based on your performance concerns).

This approach will also be less space consuming approach. Because if you accumulate history on single key, existing history will be copied to next version every time which means your every entry will consume previous_size + delta, instead of just delta.

hsnkhrmn
  • 961
  • 7
  • 20
  • Okay hsnkhrmn, Thank you for your response. But I need to store Multiple entries on one array and retrieve only one value. – Isha Padalia Jun 11 '20 at 04:39