Select specific field in MongoDB using ruby then pass the value to a variable - ruby

I'm working on a script that will return value in a specific field and exclude other fields i tried these codes:
name = 'bierc'
puts collection.find({"name"=> name},{:fields => { "_id" => 0}}).to_a
and
name = 'bierc'
collection.find("name" => name,"_id" => 0).each{|row| puts row.inspect}
These two returns
{"_id"=>BSON::ObjectId('55f0d965fcd4fe1c659cf472'), "name"=>"bierc", "song"=>"testsong"}
I want to select name only and exclude the song and especially the _id then will work on to pass the value of name field to a variable.

The option is not fields but projection. You do need _id => 0 but then 1 for any field or fields you do want to select should exclude the others.
collection.find("name" => name, projection: => { "_id" => 0, "name" => 1}})

Related

Require a field if the value exists in another field, which is an array

Say a model has a status property, that holds an array of strings. If this array contains a string name "Other", the status_other field should be required. I can achieve this with the following rules:
'status' => 'nullable|array',
'status_other' => Rule::requiredIf(in_array('Other', $this->model->status))
Is there a way to write the status_other rule as a string? I tried:
'status_other' => 'required_if:status,in:Other',
and
'status_other' => 'required_if:status,Other',
Both that did not work.
You can compare it by using '==' to match the string in the array.
'status_other' => 'required_if:status,==,Other',

Logstash Jdbc_streaming filter plugin returns an empty set using "parameters" option

I'm using the jdbc input plugin to get data from "table1":
statement => "SELECT * FROM table1 where id=1"
Result is : id:1 and id_subscriber:1
Then I'm using the jdbc_streaming filter plugin to get more data from "table2" using the "id_subscriber" field value from the previous statement, so I'm using the following statement which gets me an empty result :
statement => "SELECT * FROM table2 where id_subscriber = :idsub"
parameters => { "idsub" => "%{id_subscriber}"}
target => "arrayOfResults" #arrayOfResults is an empty array
If I use the id_subscribe value directly in the following statement, I get the four records I'm looking for :
statement => "SELECT * FROM table2 where id_subscriber = 1"
target => "arrayOfResults" # I get the right result
Can you tell me what Im I doing wrong ?
Thank you.
My bad, I did not understand how the "parameters" option works.
The right answer if someone came across this issue is simply :
`parameters => { "idsub" => "id_subscriber"}`
The right sided part of parameters was referring to the field name and not it's value.

Elasticsearch return distinct property values

I have collection of Products with property Brand and 2 unique values:
"Super brand A"
"Super brand B"
ES query
var response = new ElasticClient().Search<DTO>(s => s
.Index("index")
.Type("type")
.Aggregations(a => a
.Terms("unique", t => t
.Field(f => f.Brand)
//.Field(f => f.Brand.Suffix("keyword"))
.Size(1000)
)
)
);
var brands = (((BucketAggregate)response.Aggregations.First().Value).Items).Cast<KeyedBucket<Object>>().Select(x => x.Key).ToList();
ES returns 4 invalid values
"Super"
"brand"
"A"
"B"
I've tried to force full property match by adding .Suffix("keyword") to field but then it returns empty list. How can I get 2 distinct values?

Elastic search filter using query string

Using Query string OnFieldWithBoosts added different fields need to apply filter, string fields records are working fine.
For id field when I include .Add(id,2) it does not return ID based result.
When I use term then ID fields records working fine.
Now in the query section,
I have used OR condition, so when first condition satisfies, it does not check second one.
If I user AND condition, then it checks for both the condition matches.
But I need first query results and second query results concat into one result
CODE:
var result = client.Search<dynamic>(q => q
.Indices("test")
.Types("user")
.From(1)
.Size(10)
.MinScore(1.0)
.Fields("id", "createddate", "email", "modifieddate", "name", "companyname") // Result set Fields Fields
.Query(q1 =>
{
qq = (q1.ConstantScore(a => a.Filter(b => b.Term("id", searchKeyword))))
|| q1.QueryString(qs => qs.Query(searchKeyword).OnFieldsWithBoost(a => a.Add("notes",3).Add("email", 2).Add("name", 2)));
return qq;
})
);

How to count many _ids for the search term?

How to make query returns count numbers of post_ids for searched names? I would like to have name and count in the resulting array.
Actual code:
#array_tags = Tag.where(:name.in=>[/r/])
# returns
# [{"_id":"4eb57a20b51ab102cc00001f","name":"ruby","post_ids":["4eb57a20b51ab102cc00001e","4eb57a53b51ab102cc000023","4eb57a63b51ab102cc000025"]}]
# best expected
# [{"_id":"4eb57a20b51ab102cc00001f","name":"ruby","count":"3"}]
Schema:
{ "_id" : ObjectId( "4eb57a20b51ab102cc00001f" ),
"name" : "ruby",
"post_ids" : [
ObjectId( "4eb57a20b51ab102cc00001e" ),
ObjectId( "4eb57a53b51ab102cc000023" ),
ObjectId( "4eb57a63b51ab102cc000025" ) ] }
EDIT!
I get it! Source:
#tags = Tag.grpost(params[:term])
def self.grpost(find_by)
self.collection.group(
:key => 'name',
:cond => {:name=>{"$in"=>[/^#{find_by}/]}},
:reduce => "function(obj,prev) { prev.total_posts += obj.post_ids.length; }",
:initial => { total_posts: 0 }
)
end
I don't think it's possible to do that on-the-fly without some complex map-reduce operation.
Most probably it would be easier to add the count field to your Tag document and maintain it yourself with $inc or something.

Resources