Query compound key without the 2nd key?


#1

Let’s say I have the following documents and map function.

Document 1
    Key: parent

Document 2
    Key: child1
    parent: parent
    created_at: 2

Document 3
    Key: child2
    parent: parent
    created_at: 1


function (doc, meta) {
    if (doc.parent) {
        emit([doc.parent, doc.created_at], meta.id);
    }
}

Is it possible to query this view without passing the 2nd key (in this case doc.created_at)?

I’m trying to sort the result by doc.created_at, but I don’t know the values in my code.


#2

Well…please delete it lol

it works perfectly without passing the 2nd key. I was entering the key into ‘key’ instead of ‘startKey’


#3

Glad this worked! Just to clarify, within the view the “parent field” that is emitted is the information stored in the current document being processed by the view engine. In a workflow like:

create document child1 with parent field pointing to parent.
delte parent document
child1 and subsequently the view engine’s doc.parent field will be unchanged unless you specifically edit child1’s parent field.

The referential integrity is maintained at the application level.


#4

@tgreenstein Thank you for the explanation. I will keep that in mind.


#5

@tgreenstein I was wrong…

When I put [‘parent’] for both startKey and endKey, I get nothing.

When I put [‘parent’] only for the startKey, then I get the same result with [‘parent0000’] as well.

This is the actual data

{"total_rows":16,"rows":[
{"id":"entry.comment.fae7d036-e946-11e4-951e-080027dcfac6.default","key":["entry.creative.be2f23b4-e933-11e4-922b-080027dcfac6.default","2015-04-22 23:26:19"],"value":"entry.comment.fae7d036-e946-11e4-951e-080027dcfac6.default"},
{"id":"entry.comment.0e258566-e948-11e4-9236-080027dcfac6.default","key":["entry.creative.be2f23b4-e933-11e4-922b-080027dcfac6.default","2015-04-22 23:34:01"],"value":"entry.comment.0e258566-e948-11e4-9236-080027dcfac6.default"},
{"id":"entry.comment.68f83ae2-e948-11e4-b8c4-080027dcfac6.default","key":["entry.creative.be2f23b4-e933-11e4-922b-080027dcfac6.default","2015-04-22 23:36:33"],"value":"entry.comment.68f83ae2-e948-11e4-b8c4-080027dcfac6.default"},
{"id":"entry.comment.a248039a-e94d-11e4-9f20-080027dcfac6.default","key":["entry.creative.be2f23b4-e933-11e4-922b-080027dcfac6.default","2015-04-23 00:13:57"],"value":"entry.comment.a248039a-e94d-11e4-9f20-080027dcfac6.default"},
{"id":"entry.comment.fa6ba98a-e94f-11e4-9ee6-080027dcfac6.default","key":["entry.creative.be2f23b4-e933-11e4-922b-080027dcfac6.default","2015-04-23 00:30:44"],"value":"entry.comment.fa6ba98a-e94f-11e4-9ee6-080027dcfac6.default"},
{"id":"entry.comment.bb7df6c6-e952-11e4-beed-080027dcfac6.default","key":["entry.creative.be2f23b4-e933-11e4-922b-080027dcfac6.default","2015-04-23 00:50:27"],"value":"entry.comment.bb7df6c6-e952-11e4-beed-080027dcfac6.default"},
{"id":"entry.comment.f940ff58-ea01-11e4-9d63-080027dcfac6.default","key":["entry.creative.be2f23b4-e933-11e4-922b-080027dcfac6.default","2015-04-23 21:44:52"],"value":"entry.comment.f940ff58-ea01-11e4-9d63-080027dcfac6.default"},
{"id":"entry.comment.fcd16d24-ea01-11e4-abca-080027dcfac6.default","key":["entry.creative.be2f23b4-e933-11e4-922b-080027dcfac6.default","2015-04-23 21:44:58"],"value":"entry.comment.fcd16d24-ea01-11e4-abca-080027dcfac6.default"},
{"id":"entry.comment.22c065e4-ea02-11e4-a636-080027dcfac6.default","key":["entry.creative.be2f23b4-e933-11e4-922b-080027dcfac6.default","2015-04-23 21:46:02"],"value":"entry.comment.22c065e4-ea02-11e4-a636-080027dcfac6.default"},
{"id":"entry.comment.c39b9fa6-ea02-11e4-9315-080027dcfac6.default","key":["entry.creative.be2f23b4-e933-11e4-922b-080027dcfac6.default","2015-04-23 21:50:32"],"value":"entry.comment.c39b9fa6-ea02-11e4-9315-080027dcfac6.default"}
]
}

My query

startKey: [“entry.creative.be2f23b4-e933-11e4-922b-080027dcfac6.default”]
endKey: [“entry.creative.be2f23b4-e933-11e4-922b-080027dcfac6.default”]

Result: Nothing

My query 2

startKey: [“entry.creative.be2f23b4-e933-11e4-922b-080027dcfac6.default”, “0”]
endkey: [“entry.creative.be2f23b4-e933-11e4-922b-080027dcfac6.default”, “99999”]

Result: gets everything, but

startKey [“entry.creative.be2f23b4-e933-11e4-922b-080027dcfac6.defaultasdlkfjaksldf”] also works (it should not)

Basically, I want to convert the following sql

select * from aTable where key="entry.creative.be2f23b4-e933-11e4-922b-080027dcfac6.default" order by "the date in the compound key"

#6

Bump! someone please!?


#7

I think what you’re missing is the “query end” portion of the endKey. Try using [‘parent’] for your startKey, and [‘parent’, {}] as your endKey.


#8

@daves Thank you so much! That works!

Do you have any idea why [‘parent’, ‘’] does not work, but [‘parent’, {}] and [‘parent’, []] work?