$addToSet - Mongo functionality - ruby

If I use $addToSet to add data which was earliar deleted from the same collection, in that case will it that entry again?
Query:
XYZCollection.collection.update_one({ 'm_id' => BSON::ObjectId.from_string(m_id), 'p_id' => BSON::ObjectId.from_string(p_id) }, { '$addToSet' => { 'c_ids' => c_id } })
Example I added c_id once, then deleted it, then I tried to re-add again, what will happen, will it be added in the XYZCollection?

$addToSet behaviour:
When you add new element to array if do not exist is is added to the array:
playground adding new element:
When you add new element to array that already exist , the operation do not do anything :
playground adding element that already exists
If the field key do not exist in the document , it will be created with single array element.
playgrouund adding array field in document where the field do not yet exist
When you remove element you need to remove with $pull operation:
playground pull from array

Related

Passing multiple string as parameter through url in laravel to delete specific data from database

I want to delete multiple string data that is passed by url separated by ','.
Scenario is :
http://127.0.0.1:8888/remove/lui,kui
Through this route lui and kui data that is value of name field in database will be deleted.
My web.php file
Route::get("/remove/{name}",[MyController::class,"remove"]);
MyController.php file
use App\Models\City;
function remove($name){
}
Thanks,your suggestion will be helpful.
You can accomplish this with the following:
//The string of names
$names = "cat,cat,cat,mouse,dog,rabbit";
//We break it down, seperate them by comma, then add them to an array
//After we will get the unique values only of that array
$arrayOfNames = array_unique(explode(',', $names));
//search in the name column for any of these values
//delete if found
//note that you can change the word "name" to any other column name
City::whereIn('name', $arrayOfNames)->delete();
If you have the softdelete trait in your model, and would like to hard delete its: https://laravel.com/docs/9.x/eloquent#soft-deleting
//search in the name column for any of these values
//hard delete if found
City::whereIn('name', $arrayOfNames)->forceDelete();
You can also do an update as well if that is something you are interested in the future:
//search in the name column for any of these values
//update if found
City::whereIn('name', $arrayOfNames)->update(['age' => 123, 'updated_at' => now()]);
Hope it works well for you :)

Add Element at First Index of Collection

I have a collection of FamilyMember and I want to add a new element to the first Index of this collection, and I want to know How I could achieve this?
$family_members = FamilyMember::all();
$tmp_all_members = new FamilyMember();
$tmp_all_members->id = "all_family_member";
$tmp_all_members->name = "All Family Member";
$family_members [] = $tmp_all_members
but it adds a new element at the end of the collection.
I have tried array_unshift but it throws the error:
array_unshift(): Argument #1 ($array) must be of type array, Illuminate\Database\Eloquent\Collection given
Laravel has awesome documentation. You can find what you're looking for here: https://laravel.com/docs/9.x/collections#available-methods
Collections have a prepend method that will add an item to the beginning of the collection:
$family_members->prepend($tmp_all_members);

How to update item conditionally with branch in RethinkDB

I am trying to do simple upsert to the array field based on branch condition. However branch does not accept a reql expression as argument and I get error Expected type SELECTION but found DATUM.
This is probably some obvious thing I've missed, however I can't find any working example anywhere.
Sample source:
var userId = 'userId';
var itemId = 'itemId';
r.db('db').table('items').get(itemId).do(function(item) {
return item('elements').default([]).contains(function (element) {
return element('userId').eq(userId);
}).branch(
r.expr("Element already exist"),
//Error: Expected type SELECTION but found DATUM
item.update({
elements: item('elements').default([]).append({
userId: 'userId'
})
})
)
})
The problem here is that item is a datum, not a selection. This happens because you used r.do. The variable doesn't retain information about where the object originally came from.
A solution that might seem to work would be to write a new r.db('db').table('items').get(itemId) expression. The problem with that option is the behavior isn't atomic -- two different queries might append the same element to the 'elements' array. Instead you should write your query in the form r.db('db').table('items').get(itemId).update(function(item) { return <something>;) so that the update gets applied atomically.

RethinkDB: removing item from array in one table by value from another table

I use RethinkDB in my project and have the following table structure:
data_item {
id: "generated_thing",
slug: "slug"
}
aggregation_of_data_items {
items: ["some", "ids", "from", "data_item", "table"]
}
When I delete item from content table I want to keep data consistent - delete ID from aggregation_of_data_items.items array - is there any opportunity to do this in one request (something like $pull or $pullAll in MongoDB)?
To delete an item from an array you can do the following (this is in Python but it works in any supported language):
def remove(doc, value):
doc.replace(
lambda doc: doc.merge({"items" : doc["items"].set_difference([value])}))
Now we just need to run a query that does both, the easiest way to do this is to put them in an array:
[r.table("data_item").get(id).delete(),
remove(r.table("aggregation_of_..").get(...), id)]
.run()

In backend: how to get checkboxed items id?

How I can get ids of checked items in items list in backend. I want add extra functionality to publish function. So what I need is to get that checked ids. In (com_registracijos\controllers\lists.php) I tried add something likes this:
function publish()
{
$id = JRequest::getInt('id');
$value = JRequest::getInt('value');
$boxchecked = JRequest::getInt('boxchecked');
}
To get the ids of a list checked itemsyou should get and array instead of an int:
$arrayIDs = JRequest::getVar ( 'cid', null, 'default', 'array' );
Please, note that cid is the name of the check.
Every component, if any, of the resulting array is a checked item id.
If you need a sample, you may check delete uploaded file from an array of id when they are delted in joomla? It is doing a delete, but it will give you the idea.
Regards,

Resources