couchbase add subdocument unique array values - node.js

I have a couchbase document as
{
"last": 123,
"data": [
[0, 1.1],
[1, 2.3]
]
}
currently have code to upsert the document to change the last property and add values to the data array, however, cannot find a way to insert unique values only. I'd like to avoid fetching the whole document and doing the filtering in javascript. Is there any way in couchbase?
arrayAddUnique will fail, cause there are floats in the subarrays per couchbase docs.
.mutateIn(`document`)
.upsert("last", 234)
.arrayAppend("data", newDataArray)
.execute( ... )

Related

update an array element in mongodb

I am trying to update an array element inside a document without changing whole array.
Array elements look like this
Suppose i have to only update index 1 element's value. For that i have:
_id of the document
index 1's value ("optionalImages-624476a7bd4d2bfe6bf86e9a-1-1650025533684.jpeg")
to be updated value ("optionalImages-624476a7bd4d2bfe6bf86e9a-1-1650025534589.jpeg").
I think it can be updated by mongodb's arrayfilters but i don't get the documentation correctly.
Your help will be highly appreciated.
Query1
arrayFilters using $[m] inside the path to specify the member value that we want to change
m is the member with value 20, and we set it to 100
(instead of 20 and 100, put your "....jpg" strings)
Playmongo
update(
{"_id": {"$eq": 1}},
{"$set": {"ar.$[m]": 100}},
{"arrayFilters": [{"m": {"$eq": 20}}])
Query2
pipeline update >= MongoDB 4.2
uses map on the array to
find the member with value 20, and replaces it with 100
Playmongo
update(
{"_id": {"$eq": 1}},
[{"$set":
{"ar":
{"$map":
{"input": "$ar",
"in": {"$cond": [{"$eq": ["$$this", 20]}, 100, "$$this"]}}}}}])

Is there a way to write an Azure Cosmos Db Query that allows me to see if a db record field that is a list is a subset of a provided array

I have a list of strings and my cosmos db records have a field that is also a list of strings. I need to write a cosmos db query that will see if the list of strings in the cosmos db record field is a subset of the list of strings I provided.
example
db record:
{
"name": "Jake",
"ids": [1, 2, 3, 4]
}
otherIds: [1, 2, 3, 4, 5]
I need a query that checks if db record ids is a subset of other ids.

Query to get all Cosmos DB documents referenced by another

Assume I have the following Cosmos DB container with the possible doc type partitions:
{
"id": <string>,
"partitionKey": <string>, // Always "item"
"name": <string>
}
{
"id": <string>,
"partitionKey": <string>, // Always "group"
"items": <array[string]> // Always an array of ids for items in the "item" partition
}
I have the id of a "group" document, but I do not have the document itself. What I would like to do is perform a query which gives me all "item" documents referenced by the "group" document.
I know I can perform two queries: 1) Retrieve the "group" document, 2) Perform a query with IN clause on the "item" partition.
As I don't care about the "group" document other than getting the list of ids, is it possible to construct a single query to get me all the "item" documents I want with just the "group" document id?
You'll need to perform two queries, as there are no joins between separate documents. Even though there is support for subqueries, only correlated subqueries are currently supported (meaning, the inner subquery is referencing values from the outer query). Non-correlated subqueries are what you'd need.
Note that, even though you don't want all of the group document, you don't need to retrieve the entire document. You can project just the items property, which can then be used in your 2nd query, with something like array_contains(). Something like:
SELECT VALUE g.items
FROM g
WHERE g.id="1"
AND g.partitionKey="group"
SELECT VALUE i.name
FROM i
WHERE array_contains(<items-from-prior-query>,i.id)
AND i.partitionKey="item"
This documentation page clarifies the two subquery types and support for only correlated subqueries.

MongoDB/Mongoose query to filter all the value in an array based on their presence in a collection

I have an array lets say [1,2,3] and a collection called 'Numbers' and it has a field called 'value'. I need to retain all the values in the array which are present against the 'value' field in any document in the collection.
Example,
Test array - [1,2,3]
Numbers collection - [{value: 1}, {value: 3}]
Result should be - [1,3]
Result is that way because '2' was not present against 'value' field in any documents within 'Numbers' collection.
How do i do this?
You can try below distinct query with projection and query filter.
db.Numbers.distinct( "value", { "value": { $in: [1,2,3] } } )

Distinct values from various fields in MongoDB collection

I am using node-mongodb-native to fire mongodb queries using node js.
There is a collection name 'locations', which have following fields:
sublocality1, sublocality2, sublocality3, city.
I want to fetch overall distinct values from these fields.
Eg:
Documents:
{
'sublocality1':'a',
'sublocality2':'a',
'sublocality3': 'b',
'city': 'c'
}
{
'sublocality1':'b',
'sublocality2':'a',
'sublocality3': 'b',
'city': 'a'
}
The query should return
['a' , 'b', 'c']
I tried following:
Run distinct queries for each of the fields:
collection.distinct('sublocality1',..){},
collection.distinct('sublocality2',..){},
collection.distinct('sublocality3',..){},
collection.distinct('city',..){}
Insert the result from these queries into a list, and search for distinct items across list.
Can I optimize this? Is it possible running a single query?
You could aggregate it on the database server as below:
Group Individual document, to get the values of each intended field
in an array.
Project a field named values as the union of all the intended field
values, using the $setUnion operator.
Unwind values.
Group all the records, to get the distinct values.
Code:
Collection.aggregate([
{$group:{"_id":"$_id",
"sublocality1":{$push:"$sublocality1"},
"sublocality2":{$push:"$sublocality2"},
"sublocality3":{$push:"$sublocality3"},
"city":{$push:"$city"}}},
{$project:{"values":{$setUnion:["$sublocality1",
"$sublocality2",
"$sublocality3",
"$city"]}}},
{$unwind:"$values"},
{$group:{"_id":null,"distinct":{$addToSet:"$values"}}},
{$project:{"distinct":1,"_id":0}}
],function(err,resp){
// handle response
})
Sample o/p:
{ "distinct" : [ "c", "a", "b" ] }
If you want the results to be sorted, you could apply a sort stage in the pipeline before the final project stage.

Resources