How to concat new string value with existing value in mongoengine while doing update? - mongoengine

I want concat new string value with existing value.
suppose i have 3 document in my collection
{
"field" : "some"
}
{
"field" : "value"
}
{
"field" : "test"
}
i want to write a update query which will concat "new" with existing value . My updated documents will look like
{
"field" : "some new"
}
{
"field" : "value new"
}
{
"field" : "test new"
}

Related

How to count a field if value is not null in elasticsearch

I have indexed some documents in ElasticSearch.
Short view looks like this:
{
"tenant_id":"abcd1234"
"provider_id":"3456"
.
.
.
"doctor_summary": ["line1", "line2", "line3"] OR it could be null.
}
I want to calculate this list as 1 if not null,
query_template = {
"bool":{
"must":[
{"term":{"tenant_id.keyword":tenant_id}},
{"term":{"provider_id.keyword":provider_id}},
{"range":{"visit_date":{"gte":from_date,"lte":to_date}}},
],
"filter":[]
}
}
aggs_query = {
"doctor_summary_count":{
"value_count":{"field":"doctor_summary.keyword"}
}
}
res = CLIENT.search(index = config['elasticsearch']['abcd'],
query = query_template,
size = 10000,
aggs = aggs_query)
After calling this aggregation query, it gives result as ( size of the list * total doctor_summary field).
For example: the result of above query on above document should be 1. (As it is not null).
But it gives as 3(because list contains 3 lines.)
You can use exist query in aggregation with filter aggregation.
Query:
{
"aggs": {
"count": {
"filter": {
"exists": {
"field": "doctor_summary.keyword"
}
}
}
}
}
Response:
"aggregations" : {
"count" : {
"doc_count" : 1
}
}

How to use ellipsis (…) in nested for loop

I see this error "If duplicates are expected, use the ellipsis (...) after the value expression to enable grouping by key."
locals {
key_id = {
for x in var.security_rules :
"${x.type}" => x}
}
Is it possible to use ellipsis in a nested for this loop and how can i do it?
The error message means that var.security_rules has multiple items with the same type. For example:
variable "security_rules" {
default = [
{
type = "a"
},
{
type = "b"
},
{
type = "a"
}
]
}
We can see that there are at least 2 items with the same type, which wont be accepted as key in map. What we can do here is to group the items with the same type. This is exactly what ellipsis (...) will accomplish. So:
locals {
key_id = {
for x in var.security_rules : "${x.type}" => x... }
}
The value of key_id in this example will be:
key_id = {
"a" = [
{
"type" = "a"
},
{
"type" = "a"
},
]
"b" = [
{
"type" = "b"
},
]
}

Terraform extract account id from aws_organizations_organization.main.accounts

Given an account name, is it possible to extract the account id from
resource "aws_organizations_organization" "main" {
}
So something like:
output "account_id" {
value = "aws_organizations_organization.main.accounts[name == 'account1']"
}
account_id = 012345678901
accounts = [
{
"arn" = "arn:aws:organizations::012345678901:account/o-abc123/012345678901"
"email" = "account1#email.com"
"id" = "012345678901"
"name" = "account1"
},
{
"arn" = "arn:aws:organizations::012345678902:account/o-abc123/012345678902"
"email" = "account2#email.com"
"id" = "012345678902"
"name" = "account2"
},
{
"arn" = "arn:aws:organizations::012345678903:account/o-abc123/012345678903"
"email" = "account3#email.com"
"id" = "320413348752"
"name" = "account3"
}
]
In case anyone else stumbles upon this, I was able to solve with the following:
data "aws_organizations_organization" "main" {}
locals {
account-name = "account1"
account-index = index(data.aws_organizations_organization.main.accounts[*].name, local.account-name)
account-id = data.aws_organizations_organization.main.accounts[local.account-index].id
}
output "account_id" {
value = local.account-id
}
Theoretically, if you get version 2.21.0 or newer, you should be able to use the new aws_organizations_organization data source and filter it based on the account name. For example, though not tested:
data "aws_organizations_organization" "org" {
filter = {
name = "name"
values = ["account1"]
}
}
And then where you need the account id use data.aws_organizations_organization.org.id
You can use null_data_source for creating an email list. and then extract accounts using matchkeys.
data "aws_organizations_organization" "main" {}
data "null_data_source" "main" {
count = length(data.aws_organizations_organization.main.accounts)
inputs = {
emails = data.aws_organizations_organization.main.accounts[count.index]["email"]
}
}
output "accounts" {
value = matchkeys(data.aws_organizations_organization.main.accounts, data.null_data_source.main.*.outputs.emails, list("account1"))
}

new node produced when updating children's values

I am making an Instagram-like app with Firebase and I am having difficulties getting the profile setup activity performing as I wanted it to. The Json tree looks like this
{
"Users" : {
"1" : {
"image" : "http://something.com/something",
"name" : "person 1"
},
"2" : {
"image" : "http://someone.com/someone",
"name" : "person 2"
},
"d5nDSA5QyKU7j5XWMedYZVXTqBA2" : {
"image" : "https://firebasestorage.googleapis.com/v0/b/parkir-ngasal.appspot.com/o/Profile_images%2Fcropped-1156087565.jpg?alt=media&token=28549773-a95d-4105-9287-e4e7e454113e",
"name" : "Erik Adiguno"
}
},
"posts" : {
"post 1" : {
"content" : "test content 1",
"dPicture" : "http://something.com/something",
"picture" : "http://postimage.com/postimage",
"title" : "test title 1",
"uid" : 1,
"username" : "person 1"
},
"post 2" : {
"content" : "test content 2",
"dPicture" : "http://someone.com/someone",
"picture" : "http://postimage2.com/postimage2",
"title" : "test title 2",
"uid" : 2,
"username" : "person 2"
}
}
}
So the profile setup activity simply identify the currently logged on user and push() values to both the "image" and "name" child... But as you may already have guessed, it (of course) does not update the "username" and "dPicture" child from the "posts" node. Please see the code below
private void startSetUpAccount() {
final String name = mNameField.getText().toString().trim();
final String user_id = mAuth.getCurrentUser().getUid();
if(!TextUtils.isEmpty(name) && mImageUri !=null) {
mProgress.setMessage("Almost There...");
mProgress.show();
mProgress.setCancelable(false);
mProgress.setCanceledOnTouchOutside(false);
StorageReference filepath = mStorageImage.child(mImageUri.getLastPathSegment());
filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
final String downloadUri = taskSnapshot.getDownloadUrl().toString();
//data creation
mDatabaseUsers.child(user_id).child("name").setValue(name);
mDatabaseUsers.child(user_id).child("image").setValue(downloadUri);
mProgress.dismiss();
Toast.makeText(SetupActivity.this, "Successfully updated username and profile picture", Toast.LENGTH_LONG).show();
Intent mainActivityIntent = new Intent(SetupActivity.this, MainActivity.class);
mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainActivityIntent);
}
});
}
}
I have attempted 2 ways to my disappointment:
I tried querying ordering by "uid" child equals to the currently logged on user's id... then I got stuck because I could not get reference to the child
I tried getting the "name" and "dPicture" to retrieve value from the "Users" node instead of the "posts" node, but you can't put two nodes inside a recyclerview adapter right?
I am basically stuck and any help would be much appreciated!

SilverStripe. Search by date-range in ModelAdmin

I have date-property in my DataObject.
How can I search by date-range in ModelAdmin?
For example: "search all items where date is more than 2007-13-01 and less than 2007-17-01"
or "search all items where date is between 2007-13-01 and 2007-17-01"
For now I can search only with GreaterTranFilter or with LessThanFilter, but not with both.
class MyObject extends DataObject {
private static $db = [
"Date" => "Date",
];
private static $summary_fields = [
"Date" => "Date",
];
private static $searchable_fields = [
"Date" => [
"field" => "DateField",
"filter" => "GreaterThanFilter",
"title" => 'Date from ...'
],
];
}
Additionally search field must use a calendar(datepicker)
DateField:
default_config:
showcalendar: true
Can you give an example how to search by date-range?
There is a WithinRangeFilter, but it's not going to get you very far if you're using configuration only. This is something you really need to implement procedurally.
Add the range filters by overloading getSearchContext(), then overload getList() and check the q request param for the date ranges, and apply them to the list.
public function getSearchContext()
{
$context = parent::getSearchContext();
$context->getFields()->push(DateField::create('q[Start]','Start'));
$context->getFields()->push(DateField::create('q[End]','End'));
return $context;
}
public function getList()
{
$list = parent::getList();
$params = $this->getRequest()->requestVar('q');
$filters = [];
if(isset($params['Start'])) {
$filters['Date:LessThanOrEqual'] = $params['Start'];
}
if(isset($params['End'])) {
$filters['Date:GreaterThanOrEqual'] = $params['End'];
}
return $list->filter($filters);
}

Resources