How to remove an item in an array in Firebase database? - java

I am sending an object to Firebase which has an ArrayList of users. I need to delete a user from this array. How can I do to know the position of the user in this array and delete it?
"room" : {
"-LswnaENRCSpJdB8-ZBg" : {
"description" : "",
"members" : [ {
"id" : "jh4Ch9rBgQPwBTfv...",
"name" : "John",
}, {
"id" : "5P6DzPIEuiSKU5UY...",
"name" : "Jack",
}, {
"id" : "nKTlIyaDc3O3gxNp...",
"name" : "Mary",
} ],
"name" : "MyRoom"
},
Thanks in advance!

How can I do to know the position of the user in this array and delete it?
There is currently no way you can create a query that can help you find a specific user that exists at a particular index in your members array.
To actually remove a particular user you'll need to load the entire members array, remove the user that you want client-side, and then write the entire array back.

Related

GlobalSecondary index in DynamoDB

I have to create a Global Secondary Index in Dynamo Db. My Primary table structure is below -
{
"primaryId" : "1234" //HashKey
"dummy1" : "kkd",
"dummy2" : "ddd",
"secondObj": [{
"secondObjId" : "1234",
"name" : "1234",
},
{
"secondObjId" : "12345",
"name" : "12345",
}]
}
Now i have to create GlobalSecondary Index based on "secondObjId" as a hashkey. is it possible to create?
I have created it using AWS console but its showing item count 0 and if i am creating GlobalSecondaryIndex using "dumy1" then its showing proper item count.
So my query is that is it possible to create a GlobalSecondayIndex based on a attribute from DynamoDBDocument?
Indexes can be built only on top-level JSON attributes. In addition, range keys must be scalar values in DynamoDB (one of String, Number, Binary, or Boolean).

Query a list in mongo

I have a mongo collection 'Student' with below documents
{
"_id" : ObjectId("5ccc2cded71acf061de1c2d8"),
"studentId" : "123",
"name" : "1",
"age" : NumberLong(0),
"section" : "A",
"state" : "State1",
"city" : "City1"
}
I have 100 documents with the above structure. Now i have a list with below structure
[{
"studentId": "123",
"state": "state1"
},
{
"studentId": "456",
"state": "state2"
}]
Is there any way in mongo that i can get the documents matching this list data in single db call. Iterating over list with criteria as studentId:123 and state:state1 will work but is how to get all list data without iterating in java?
All you need is a simple find query:
db.collection.find({$or: arr});
when arr is the sample array you showed.
You should note that mongo searches are case sensitive meaning with the sample array you gave no matches will be found since "state1" is not equal to "State1".

How to Compare and fetch the Json records having same Name in descending order

I have a json with n number of objects I have to fetch "id" attribute from last two objects. Also I have to first compare Id attribute as per the latest added objects to json. Say Id 16941 is to be fetched first and then id 16940 has to be fetched second.
[
"id" : 16940,
"version" : 0,
"direction" : "B",
}, {
"id" : 16941,
"version" : 0,
"direction" : "S",
} ]

Dynamodb How to perform update in a list element in an item?

I am storing data in a list element. That list attribute consists of multiple fields like year and name. Is there any way I can delete only the rows where year is 2018 like in relational databases??
Data is stored in this manner in which ID is primary key and some other fields as password, city etc. Below is how data is stored in quarters.
{
"q1risk" : "0",
"q1targets" : [ ]
}
UpdateItem succeeded:
{
"q2risk" : "100.0",
"q2targets" : [ {
"level" : "Basic",
"year" : "2017",
"name" : "1",
"completed" : "0",
"category" : "US"
}, {
"level" : "Basic",
"year" : "2017",
"name" : "2",
"completed" : "0",
"category" : "US"
}, {
"level" : "Basic",
"year" : "2018",
"name" : "3",
"completed" : "0",
"category" : "US"
} ]
}
Taken from this answer to a similar question, according to the DynamoDB UpdateExpression documentation, you can remove individual elements from the list using REMOVE ListAttribute[index].
However, there doesn't appear to be a way to conditionally delete list elements based on a condition.
Your best bet to minimize read/write throughput is to:
Query for the item with a CONTAINS filter condition and a projection expression on the list attribute.
If the item is found, it contains at least one element you want to remove, so find the indices of elements you want to remove.
Use REMOVE with the indices to remove those elements from the item's list attribute.

How to update embedded mongo document using spring mongo data api

I need some help updating property of embedded collection with JSON structure below -
translation
{
"_id" : ObjectId("533d4c73d86b8977fda970a9"),
"_class" : "com.xxx.xxx.translation.domain.Translation",
"locales" : [
{
"_id" : "en-US",
"description" : "English (United States)",
"isActive" : true
},
{
"_id" : "pt-BR",
"description" : "Portuguese (Brazil)",
"isActive" : true
},
{
"_id" : "nl-NL",
"description" : "Dutch (Netherlands)",
"isActive" : true
}
],
"screens" : [
{
"_id" : "caseCodes",
"dictionary" : [
{
"key" : "CS_CAT1",
"parameterizedValue" : "My investigations",
"locale" : "en-US"
},
{
"key" : "MY_INVESTIGATIONS",
"parameterizedValue" : "",
"locale" : "pt-BR"
},
}
]
}
In above structure:
I want to update "parameterizedValue" usinng spring-data-mongo-db API 1.3.4, for screen with _id="caseCodes" and key = "CS_CAT1".
I tried (here 'values' is an collection name for TranslationValue array)
mongoOperations.updateFirst(Query.query(Criteria.where("screens._id")
.is("caseCodes")), new Update().push(
"screens.dictionary.$.values", translationValue),
Translation.class);
but it said, "can't append array to string "dictionary"....
Any pointers or help here? Thanks.
-Sanjeev
There are a few problems with your logic as well as problems with your schema for this type of update.
Firstly what you have are nested arrays, and this causes a problem with updates as described in the documentation for the positional $ operator. What this means is that any condition matching an element of an array on the query side of the update statement will only match the first array index found.
Since you need a specific entry in the inner array you would need to match that as well. But the "catch" says that only the first match will be used in the positional operator so you cannot have both. The query form (if it were possible to work, which it does not) would actually be something like this (native shell):
db.collection.update(
{
"screens._id": "caseCodes",
"screens.dictionary.key": "CS_CAT1"
},
{
"$set": {
"screens.$.dictionary.$.parameterizedValue": "new value"
}
}
)
Now that would "appear" to be more correct than what you are doing, but of course this fails because the positional operator cannot be used more than once. I may just quite stupidly work in this case as it just so happens that the first matched index of the "screens" array (which is 0) happens to be exactly the same as the required index of the inner element. But really that is just "dumb luck".
To illustrate better, what you need to do with these type of updates is already know the indexes of the elements and place those values directly into the update statement using "dot notation". So updating your second "dictionary" element would go like this:
db.collection.update(
{
"screens._id": "caseCodes",
"screens.dictionary.key": "MY_INVESTIGATIONS"
},
{
"$set": {
"screens.0.dictionary.1.parameterizedValue": "new value"
}
}
)
Also noting that the correct operator to use here is $set as you are not appending to either of the arrays, but rather you wish to change an element.
Since that sort of exactness in updates is unlikely to suit what you need, then you should look at changing the schema to accommodate your operations in a much more supported way. So one possibility is that your "screens" data may not possibly need to be an array, and you could change that to a document form like so:
"screens" : {
"caseCodes": [
{
"key" : "CS_CAT1",
"parameterizedValue" : "My investigations",
"locale" : "en-US"
},
{
"key" : "MY_INVESTIGATIONS",
"parameterizedValue" : "",
"locale" : "pt-BR"
},
]
}
This changed the form to:
db.collection.update(
{
"screens.caseCodes.key": "CS_CAT1"
},
{
"$set": {
"screens.caseCodes.$.parameterizedValue": "new value"
}
}
)
That may or may not work for your purposes, but you either live with the limitations of using a nested array or otherwise change your schema in some way.

Categories