Search code examples
rethinkdbrethinkdb-javascript

rethinkdb, How could I pluck the result by a value in particular "array index"?


sample data

[
  {
    "createdDate": 1508588333821,
    "data": {
      "image_extension": "png",
      "name": "Golden",
      "qty": 1,
      "remark": "#296-2",
      "status": "RETURN",
      "owner": [
        {
          "name": "app1emaker",
          "location": 1
        },
        {
          "name": "simss92_lmao",
          "location": 31
        }        
      ]
    },
    "deleted": false,
    "docId": 307,
    "docType": "product",
    "id": "db0131f9-9359-4aa3-b6ed-cd9f3ff4aa3e",
    "updatedDate": 1553155281691
  },
  {
    "createdDate": 1508588333324,
    "data": {
      "image_extension": "png",
      "name": "Golden",
      "qty": 1,
      "remark": "#296-2",
      "status": "DISCARD",
      "owner": [
        {
          "name": "At533",
          "location": 7
        },
        {
          "name": "madsimon",
          "location": 64
        },
        {
          "name": "boyboy96",
          "location": 1
        },
        {
          "name": "xinfengCN",
          "location": 5
        }
      ]
    },
    "deleted": false,
    "docId": 308,
    "docType": "product",
    "id": "3790bdaa-5347-4ab0-8149-37332c23c6ea",
    "updatedDate": 1554555231691
  },
  ...
  ...
]

And said that, I would like to select the data.owner on array index 0 only (or I should say data.owner[0]), which are

{
  "name": "app1emaker",
  "location": 1
}

and

{
  "name": "At533",
  "location": 7
}

in this case. I have a failed code below.

r.db('carbon').table("items").pluck(['id', 'docId', 'createdDate',{data:{name: true, owner:[0]}}])

I saw that for some functions like orderBy, rethinkdb allowed to use orderBy(r.row('data')('owner')(0)('name')) for access nested object, but I have no idea how to do this for pluck? could anyone give me some hints? Thanks a lot


Solution

  • pluck can not do that, but you can fall back to use the map:

    r.db("carbon").table("items").map(function(doc){
      return {
        "id": doc("id"), 
        "docId": doc("docId"),
        "createdDate": doc("createdDate"),
        "data": {
          "name": doc("data")("name"),
          "owner": doc("data")("owner")(0)
        }
      }
    })