How do you query a CouchDB view that emits complex keys? - couchdb

Given a CouchDB view that emits keys of the following format:
[ "part1", { "property": "part2" } ]
How can you find all documents with a given value for part1?
If part2 was a simple string rather than an object startkey=["part1"]&endkey=["part1",{}] would work. The CouchDB docs state the following:
The query startkey=["foo"]&endkey=["foo",{}] will match most array keys with "foo" in the first element, such as ["foo","bar"] and ["foo",["bar","baz"]]. However it will not match ["foo",{"an":"object"}]
Unfortunately, the documentation doesn't offer any suggestion on how to deal with such keys.

The second element of your endkey value needs to be an object that collates after any possible value of the second element of your key. Objects are compared by property-by-property (for example, {"a":1} < {"a":2} < {"b":1}) so the best way to do this is to set the first property name in your endkey to a very large value:
startkey=["part1"]&endkey=["part1", { "\uFFF0": false }]
The property name of \uFFF0 should collate after any other property names in the second key element, and even works when the second element is an empty object or has more than one property.

Related

Update specific field in an array of object in a MongoDb Document

I have a document with that format :
{
"field1":"string",
"field2":123,
"field3":
[
{
"subField1":"string",
"subField2": object,
// other fields after
},
{
"subField1":"string",
"subField2": object,
// other fields after
},
// other fields after
]
// other fields after
}
I want to update at once the field3.$.subField2 field only, with a different value in each of the elements of the array composing field3.
Does anyone knows how to properly do it ?
One reason I want to do this is I have several asynchronous operations that are meant to update differents fields of the array field, and I have concurrency issues...
I tried with findOneAndUpdate(query, $set{"field3.$.subField2": arrayOfValues}) but it does not works, it seems I can only pass one single value that would be set to each element of the array (all the same).
arrayOfValues would be of course an array with only the values I want with the matching indexes and same size of the document's array.

How do I delete a string object element stored in Redis?

I am trying to delete an entire object stored in Redis using LREM, but I don't think I am doing it right, below is the code I tried:
GraphQL mutation:
deleteImage: async(_,args) => {
let data = await client.lrem("postedImagesList",0,JSON.stringify(args.id));
return data
}
postedImagesList is a redis collection of all the object elements stringified:
1) "{\"id\":\"aadc8456-a5c9-46b9-a58c-670446d95c70\",\"url\":\"chas\",\"description\":\"asd\",\"posterName\":\"asda\",\"binned\":false,\"userPosted\":true}"
2) "{\"id\":\"a24d6d1b-224f-400d-8b84-dac83d8eaf92\",\"url\":\"ihir\",\"description\":\"helped\",\"posterName\":\"bhi\",\"binned\":false,\"userPosted\":true}"
I am given the id as an argument and I have to delete the entire object, how can I do so? I am adding the elements using lpushasync.
Please see the LREM docs.
LREM key count element
The element argument has to have an exact match, so you can't match by ID.
You might want to consider using the RedisJSON using the JSON.DEL you should be easily achieve what you're looking for.

Updating firestore document nested data overwrites it

I'm trying to set some new fields in a nested dict within a Firestore document, which results in the data being overwritten.
Here's where I write the first part of the info I need:
upd = {
"idOffer": {
<offerId> : {
"ref" : <ref>,
"value" : <value>
}
}
}
<documentRef>.update(upd)
So output here is something like:
<documentid>:{idOffer:{<offerId>:{ref:<ref>, value:<value>}}}
Then I use this code to add some fields to the current <offerId> nested data:
approval = {
"isApproved" : <bool>,
"dateApproved" : <date>,
"fullApproval" : <bool>
}
<documentRef>.update({
"idOffer.<offerId>" : approval
})
From which I expect to get:
<documentid>:{idOffer:{<offerId>:{ref:<ref>, value:<value>, isApproved:<bool>,dateApproved:<date>,fullApproval:<bool>}}}
But I end up with:
<documentid>:{idOffer:{<offerId>:{isApproved:<bool>,dateApproved:<date>,fullApproval:<bool>}}}
Note: I use <> to refer to dynamic data, like document Ids or References.
When you call update with a dictionary (or map, or object, or whatever key/value pair structure used in other languages), the entire set of data behind the given top-level keys are going to be replaced. So, if you call update with a key of idOffer.<offerId>, then everything under that key is going to be replaced, while every other child key of the idOffer level will remain unchanged.
If you don't want to replace the entire object behind the key, then be more specific about which children you'd like to update. In your example, instead of updating a single idOffer.<offerId> key, specify three keys for the nested children:
idOffer.<offerId>.isApproved
idOffer.<offerId>.dateApproved
idOffer.<offerId>.fullApproval
That is to say, the dictionary you pass should have three keyed entries like this at the top level, rather than a single key of idOffer.<offerId>.

Dynamodb querying for list count

I have a dynamodb table which has following columns,
id,name,events, deadline
events is a list which contain number of events.
I want to scan/query for all the rows with following items as the result,
id, name, number of events.
I tried following way but didn't receive any value for number of events. Can someone show me where am I wrong.
var params = {
TableName: 'table_name',
ExpressionAttributeNames: {"#name": "name",
"#even": "events.length"
},
ProjectionExpression: 'id, #name, #even'
}
You cannot achieve what you want in this way. The entries in "ExpressionAttributeNames" are not evaluated as expressions.
The definition of "#even": "events.length" in "ExpressionAttributeNames" does not evaluate the expression event.length and assign it to the variable "#even". Instead it specifies "#even" as referring to a column named "events.length" or a table where "events" is an object that has a "length" attribute. Since your table has neither, you get nothing back.
From the DynamoDB documentation:
In an expression, a dot (".") is interpreted as a separator character in a document path. However, DynamoDB also allows you to use a dot character as part of an attribute name.
To achieve what you want, you will have to return the "events" column and calculate the length outside of the query, or define a new "eventsLength" column and populate and maintain that value yourself if you are concerned about returning "events" in each query.

Mongo DB C# code using 2 level , 3 level and 4 level of array

I have to update in C# code using MongoDB. Here I had implement 2nd level array of update in below (subBranchindex is taken in a generic list object):-
for (var index = 0; index < subBranchindex.Count; index++)
{
if (subBranchindex[index]._id == new ObjectId(subBranchid))
{
IMongoQuery queryEdit = Query.EQ("BranchOffice.SubBranchlist._id", new ObjectId(subBranchid));
UpdateBuilder update = Update.Set("BranchOffice.$.SubBranchlist."+ index +".Name",subBranch.SubName).
SafeModeResult s = dc.Collection.Update(queryEdit, update,
UpdateFlags.None, SafeMode.True);
}
}
Here 2nd level array, I was using (for loop Statement) to taken Index value for array. Next I can use 3rd, 4th and 5th level of array means more than (for loop statement) will be assign. So don't need [for loop Statement] and also don't need to assign hardcore number in index.
For example: ("BranchOffice.$.SubBranchlist.0.Name",subBranch.SubName). Here Don't Hardcore number[index] 0 or 1 or 2. "2nd" level array more than 100 record is there.
Is there any way I can use to array index value? Please explain how to solve this probelm. Please explain me with Example.
Based on your example above, my understanding of your schema is the following:
The top-level document has a BranchOffice field
BranchOffice is an array of objects
Each object within BranchOffice has an _id, SubName and SubBranchlist field
SubBranchlist is an array of objects
Each object within SubBranchlist has a Name field
Your update statement appears to be copying the SubName field to each Name field among objects within SubBranchlist (a sibling field of SubName).
Using the property path syntax to select fields through arrays (e.g. SubBranchlist.0.Name), there is no "wildcard" index that will allow you to modify Name fields among all objects in the array.
On a somewhat related note, the $ positional operator only applies to the first-matched array element, so you cannot use that to update multiple array elements. In your case, it would not be an option anyway, since you're using the positional operator for the BranchOffice array field.
You can either issue a series of update queries (for each element in SubBranchlist), or consider using $set to modify the entire SubBranchlist array in one query. The downside with using $set is that you'll need to read and write back the entire array, which may be a problem if other, concurrent operations are also issuing updates to the array.

Resources