I’m trying to query data using n1ql and mutate the data using subdocs and am trying to figure out the best way to store the documents.
Let’s say I have a document that has “people” in it (along with lots of other fields). I want to be able to search by a person and perform crud operation on a person (on a subdoc level).
The 2 ways I came up with are:
{
lots of other info
"people" : {
"bob" : { stuff about bob },
"jim" : {stuff about jim}
}
}
OR:
{
lots of other info
"people" : [
{"name" : "bob", stuff about bob },
{"name" : "jim", stuff about jim}
]
}
For choice 1: (having the id as the “key”). I can’t seem to write a good n1ql query that uses indexes. To find the documents that “jim” is in, I can query using “people.jim IS VALUED”, but that doesn’t use indexes (and I can’t use a prepared statement).
For choice 2 (having the name as a field in an array). Indexes will work fine, but the subdoc crud operations aren’t as clean.
For instance, when I want to do an create, update or delete, I would need to select the whole list, modify it then “put” it (and worry about cas).
Using option 1, I would only to do an upsert or delete a subdoc using the person’s name (atomic and super easy).
Am I missing something in either n1ql or the subdoc api that would make this easier (or an entirely different way to store the doc)
Geraldss,
That’s great news!!
Choice 1 is the one I’d like to go with.
We are currently on 4.5.0, but I could upgrade.
What would the indexes (and query) look like?