mongodb ruby :Updating a field within a nested document - ruby

I have a document in mongodb with the following structure.
{
"_id" : ObjectId("52ad477a5873b710c0000007"),
"ad_name" : "name_6",
"selected" : true,
"vendor" : "facebook.com",
"status" : "VALID",
"ad_details" : {
"Body" : "something",
"Title" : "random_6",
"Url" : "www.someurl.com",
"Image" : "blob_data"
}
}
i want to update the field name "selected" and the field "Url" in one query.Currently i can update selected field but not Url.
ads.update({"_id" => BSON::ObjectId.from_string(doc_id)},
{"$set"=> {"selected" => select, "Url" => params["urls"][indx]}}
This query does not give me any error but doesnt update the Url as it is present inside ad_details sub-document. Also i DO NOT WANT to update entire ad_details json which is one way of doing it.Please help!!

You just need to specify the path to the value you want to update:
'$set' => { 'ad_details.Url' => params['urls'][idx], ... }

Related

document field returns null when querying groups of Prismic Content-Realtionship fields in graphql

Issue:
I am using Prismic to send data through to my website.
In Prismic I have a Type (testimonial_list) that consists of a group of content-relation fields (Prismic Type testimonials).
To query the data on the inner Types I need to access them via the document field in graphql and use inline-fragments.
I have followed as instructed here:
https://github.com/angeloashmore/gatsby-source-prismic#Query-Content-Relation-fields
Inside graphql I have managed to navigate to the testimonial data-fields (on the document field) but the document field returns null, this is where I'm stuck. I can't work out why it would return null as the content exists and the fields are clearly being found in graphql.
Info:
My project is built using Gatsby and I'm using the plugin gatsby-source-prismic v3.1.1
Here you can see I can access the correct field data and I am getting the right number of nodes returned but document is empty:
This is the JSON for the testimonial_list Type on Prismic:
{
"Main" : {
"prismic_title" : {
"type" : "StructuredText",
"config" : {
"single" : "heading6",
"label" : "Title (only used to name entry in Prismic list)",
"placeholder" : "Prismic list title (otherwise \"undefined\")"
}
},
"page" : {
"type" : "Select",
"config" : {
"options" : [ "Homepage", "Option 2", "Option 3" ],
"label" : "Website page to appear on:"
}
},
"testimonial_list" : {
"type" : "Group",
"config" : {
"fields" : {
"testimonial" : {
"type" : "Link",
"config" : {
"select" : "document",
"customtypes" : [ "testimonial" ],
"label" : "testimonial"
}
}
},
"label" : "Testimonial List"
}
}
}
}
Thank you for any help, if there is any more info I can supply to help deduce the issue please let me know.
In the end, the issue turned out to be a typo in my gatsby-config where I was requiring the schema.
It was a daft mistake but stare at something too long and these things happen I guess.
In case anybody else has a similar issue you must ensure the property names of your Prismic schemas inside your gatsby-config are exactly the same as in Prismic.
For example if your Type in Prismic is called "my_type" then you must use that exact syntax - so for example don't use "myType".
Hey it might be something related to the gatsby-source-prismic plugin
I would directly open an issue for it here if I were you: https://github.com/angeloashmore/gatsby-source-prismic/issues

How to store nested document as String in elastic search

Context:
1) We are building a CDC pipeline (using kafka & connect framework)
2) We are using debezium for capturing mysql Tx logs
3) We are using Elastic Search connector to add documents to ES index
Sample change event generated by Debezium:
{
"source" : {
"before" : {
"Id" : 97,
"name" : "Northland",
"code" : "NTL",
"country_id" : 6,
"is_business_mapped" : 0
},
"after" : {
"Id" : 97,
"name" : "Northland",
"code" : "NTL",
"country_id" : 6,
"is_business_mapped" : 1
},
"source" : {
"version" : "0.7.5",
"name" : "__",
"server_id" : 252639387,
"ts_sec" : 1547805940,
"gtid" : null,
"file" : "mysql-bin-changelog.000570",
"pos" : 236,
"row" : 0,
"snapshot" : false,
"thread" : 614,
"db" : "bazaarify",
"table" : "state"
},
"op" : "u",
"ts_ms" : 1547805939683
}
What we want :
We want to visualize only 3 columns in kibana :
1) before - containing the nested JSON as string
2) after - containing the nested JSON as string
3) source - containing the nested JSON as string
I can think below possibilities here :
a) Either converting nested JSON as string
b) Combining column data in elastic search
I am a newbie to elastic search . Can someone please guide me how to do that.
I tried defining custom mapping as well but it is giving me exception.
You can always view your document as a Raw JSON in Kibana.
You don't need to manipulate it before indexing in elastic.
As this is related to visualization, handle this in Kibana only.
Check this link for a screenshot.
Refer this to add the columns which you want to see onto the results
I don't fully understand your use case, but if you would like to turn some json's to their representing strings, then you can use logstash for that, or even Elasticsearch ingest capabilities to convert an object (json) to a string.
From the link above, an example:
PUT _ingest/pipeline/my-pipeline-id { "description": "converts the
content of the id field to an integer", "processors" : [
{
"convert" : {
"field" : "source",
"type": "string"
}
} ] }

How to insert an element into already present list in elastic search

Say I have documents stored like below.
document 1
{
id : '1',
title : "This is a test document1",
list : ["value1" , "value2"],
...
}
document 2
{
id : '2',
title : "This is a test document2",
valueList : ["value1" , "value2"],
...
}
I need to add some more elements to the valueList in the documents with a list of document ids using bulk api. The resulting should look like
document 1
{
id : '1',
title : "This is a test document1",
list : ["value1" , "value2", "value3"],
...
}
document 2
{
id : '2',
title : "This is a test document2",
valueList : ["value1" , "value2" , "value3"],
...
}
What can I do to achieve this?
I tried using the scripts but it only updates a single document.
Sorry am really new to elastic search. I could even be stupid on this question. Please forgive and make me clear with this question.
See Updating Document. It should be straightforward. You need to use _update and just to give you an idea, even though the documentation is nearly perfect, it could look like this:
POST /your_index/your_type/document1/_update
{
id : '1',
title : "This is a test document1",
list : ["value1" , "value2", "value3"]
}
This will update document1.
In case of bulk updates you should read Batch Processing and have a look at the Bulk API.
From the docs:
POST /your_index/your_type/_bulk
{ "update" : {"_id" : "document1", "_type" : "your_type", "_index" : "your_index"}}
{ "doc" : {"myfield" : "newvalue"} }
{ "update" : {"_id" : "document2", "_type" : "your_type", "_index" : "your_index"}}
{ "doc" : {"myfield" : "newvalue"} }
Please note that you can just use _update for Partial Updates.
The simplest form of the update request accepts a partial document as
the doc parameter, which just gets merged with the existing document.
Objects are merged together, existing scalar fields are overwritten,
and new fields are added.

Elastic search Update by Query to Update Complex Document

I have a use case of elastic search to update a doc.
My doc is something like this-
{
"first_name" : "firstName",
"last_name" : "lastName",
"version" : 1234,
"user_roles" : {
"version" : 12345,
"id" : 1234,
"name" : "role1"},
},
"groups" : {
"version" : 123,
"list": [
{"id":123, "name" : "ashd"},
{"id":1234, "name" : "awshd"},
]
}
}
Now depepeding on some feed I will either will be updating the parent doc or will be updating the nested doc.
I am able to find how to update the basic attributes like firstName and lastName but unable to get how to update complex/nested ones
I did something like from REST client-
"script": {
"inline": "ctx._source.user_roles = { "id" : 5678, "name" :"hcsdl"}
}
but its giving me exception-
Actual use case-
I will actually be getting a Map in java.
This key can be simple key like "first_name" or can be complex key like "user_role" and "groups"
I want to update the document using update by query on version.
The code I wrote is something like-
for (String key : document.keySet()) {
String value = defaultObjectMapper.writeValueAsString(document.get(key));
scriptBuilder.append("ctx._source.");
scriptBuilder.append(key);
scriptBuilder.append('=');
scriptBuilder.append(value);
scriptBuilder.append(";");
}
where document is the Map
Now I might get the simple fields to update or complex object.
I tried giving keys like user_roles.id and user_roles.name and also tried giving complete user_roles but nothing is working.
Can someone helpout
Try this with groovy maps instead of verbatim JSON inside your script:
"script": {
"inline": "ctx._source.user_roles = [ 'id' : 5678, 'name' : 'hcsdl']}
}

Querying for specific strings in Firebase?

I have a database which looks something like this:
"trucks" : {
"3705ec54-8a2e-4eb1-8bb9-ab2243645ac1" : {
"email" : "sandwiches#123.com",
"name" : "Sandwich Truck",
"phone" : "123 - 456 - 1234",
"provider" : "password",
"selfDescription" : "We serve delicious sandwiches at a moderate price. Cards Accepted.",
"userType" : "truck",
"website" : "www.sandwiches.com"
},
"54fea8cd-2203-46bd-aaf8-9d823e85313d" : {
"email" : "pizza#123.com",
"name" : "Supa Pizza",
"phone" : "619 - 222 - 4444",
"provider" : "password",
"selfDescription" : "We serve incredible pizza at an incredibly unfair price.",
"userType" : "truck",
"website" : ""
},
"6c542367-507c-4d01-af2c-bf93a7efaef4" : {
"email" : "fries#123.com",
"name" : "Pete's Fries",
"phone" : "11111111111111111111",
"profilePhoto" : "",
"provider" : "password",
"selfDescription" : "We make some of the world's most delicious fries.",
"userType" : "truck",
"website" : ""
},
"7c6c4395-aec1-443c-908d-62db517def5e" : {
"email" : "chili#123.com",
"name" : "Mark's Chili",
"phone" : "1-800-CHIL-LLL",
"profilePhoto" : "",
"provider" : "password",
"selfDescription" : "We serve the most delicious chili, chili your mamma's mamma is scared to try.",
"userType" : "truck",
"website" : ""
}
}
I'd like to implement a search bar for which trucks whose names match the search terms will be returned. For example, any truck name containing the string 'fries' will have its ID committed to an array of search result IDs.
Here's what I've tried so far, but no dice. When I type in fries in my searchbar and hit the search button, it does not print "6c542367-507c-4d01-af2c-bf93a7efaef4"
let usersRef = Firebase(url: "https://•••••••••.firebaseIO.com/users")
usersRef.queryOrderedByChild("name").queryEqualToValue(searchBar.text).observeEventType(.ChildAdded, withBlock:{
snapshot in
print(snapshot.key)
})
I'm not even sure how close I am, any help would be massively appreciated.
Thanks!
You are querying using queryEqualToValue (Query.equalTo()), which will return result only if there is an exact match.
Explanation in Detail:
In the search bar if we are entering "fries" it will look for an exact match of "fries", which is not available in our document and hence the desired value "6c542367-507c-4d01-af2c-bf93a7efaef4" is not printed.
Instead if we give the value "Pete's Fries" in the search bar then we will get the result "6c542367-507c-4d01-af2c-bf93a7efaef4"
If we give partial value for our search, then we are trying to implement a search which is similar to LIKE Query in SQL. Please refer the below posts to get more info on "how to perform LIKE query in firebase"
How to perform sql "LIKE" operation on firebase?

Resources