Issues with embedded mongoDB print/query - mongodb-query

I am trying to do a query that prints out all values of "x" that equal 2 in "initialSelected"
Values can look like this with different values of x and y
"initialSelected" : {
"x" : 2,
"y" : 2
}
I can get items to print with
db.main.find({ "initialSelected" : { "x" : 2, "y" : 2 } }).pretty()
But when I remove the "y" ..
db.main.find({ "initialSelected" : { "x" : 2 } }).pretty()
nothing will print
I thought about making the y value $lt and $gt 0 for all values of y since it seems it needs to be addressed, however the simple query below wouldn't print anything either.
db.main.find({ "initialSelected" : { "x" : 2, "y" : { $eq : 2 } } }).count()
What am I missing? I am new to this. Thanks!

A query of db.main.find({ "initialSelected" : { "x" : 2 } }) will only match documents whose initialSelected field is an exact match to { x: 2 } (i.e. no y field).
You need to use dot notation to match just the x field:
db.main.find({ "initialSelected.x" : 2 })

Related

Kusto query : how to replace empty or blank values(rows) under a column with a string like 'unknown'?

Hope someone can guide me here; how to replace and rename a blank or empty value under a column to a string like 'unknown, I'm basically looking for a simple line of code like:
|extend new_col = replace(#'', #'unknown', col1) or
if (isNull(country_code), "unknown", country_code)
you could try using iff or case as follows:
datatable(s:string)
[
"hello",
"",
"world",
""
]
| project s = case(isempty(s), "unknown", s)
// or
datatable(i:int)
[
1,
int(null),
2,
int(null)
]
| project s = case(isnull(i), -99999, i)

Set a json objects values from an array in postgresql

I have an Array of JSON objects in PostgreSQL inside data.json, That looks like this
[
{ "10" : [1,2,3,4,5] },
{ "8" : [5,4,3,1,2] },
{ "11" : [1,3,5,4,2] }
]
The data is taken from a select statement
SELECT
ARRAY((SELECT json_build_object(station_id,
ARRAY((SELECT
COALESCE((SELECT SUM(prodtakt_takt)
FROM psproductivity.prodtakt
WHERE prodtakt_start::date = generate_series.shipping_day_sort::date
AND station_id = prodtakt_station_id
),0)
FROM generate_series)) ) FROM psproductivity.station WHERE (SELECT COALESCE(SUM(prodtakt_takt),0) FROM psproductivity.prodtakt WHERE station_id = prodtakt_station_id) > 0
)) AS json, ...
Where generate_series is just a series of dates.
Now I need to that and turn it into this format of a JSON object
{
"x" : "x",
"jsondata" : {
"10" : [1,2,3,4,5]
"8" : [5,4,3,1,2]
"11" : [1,3,5,4,2]
}
}
the software I am working on uses c3.js to process data into graphs so I have to change this format. I am thinking that I need to start with something like
json_build_object( 'jsondata',( SELECT FROM json_each(unnest(data.json)) ) )
But I can think of no route with that logic. Adding the x into the JSON object is easy. I am confident I can do that part if I can just reorganize the array

Find distinct length of string field using mongo shell

Given a mongo collection like:
col1 col2
1 "mango"
2 "banana"
3 "watermelon"
4 "orange"
How do I get the length of distinct string lengths of column col2? It would probably be using the strLenCP function but am not able to construct it just for the projection.
Expected output would be:
(5, 6, 10)
since the distinct string lengths of (banana, orange) are 6, watermelon 10 and mango 5.
You can do this with an aggregation pipeline by using $strLenCP within a $group:
db.test.aggregate([
// Group documents by their col2 string length
{$group: {_id: {$strLenCP: '$col2'}}}
])
Output:
{ "_id" : 10 }
{ "_id" : 6 }
{ "_id" : 5 }

return 0 instead of nothing for attribute value that is not available in any collections

I have a document as follow:
{
"_id" : ObjectId("5491d65bf315c2726a19ffe0"),
"tweetID" : NumberLong(535063274220687360),
"tweetText" : "19 RT Toronto #SunNewsNetwork: WATCH: When it comes to taxes, regulations, and economic freedom, is Canada more \"American\" than America? http://t.co/D?",
"retweetCount" : 1,
"Added" : ISODate("2014-11-19T04:00:00.000Z"),
"tweetLat" : 0,
"tweetLon" : 0,
"url" : "http://t.co/DH0xj0YBwD ",
"sentiment" : 18
}
now I want to get all document like this where Added is between 2014-11-19 and 2014-11-23 but we should note that there might be no data in for example this date : 2014-11-21 and now the problem starts: here when this happens I want 0 for sum of sentiment for this date instead of returning nothing( I know I can check this in java but it is not reasonable), my code is as follow which works fine except for the date that is not available it returns nothing instead of 0:
andArray.add(new BasicDBObject("Added", new BasicDBObject("$gte",
startDate)));
andArray.add(new BasicDBObject("Added", new BasicDBObject("$lte",
endDate)));
DBObject where = new BasicDBObject("$match", new BasicDBObject("$and",
andArray));
stages.add(where);
DBObject groupFields = new BasicDBObject("_id", "$Added");
groupFields.put("value",
new BasicDBObject("$avg", "$sentiment"));
DBObject groupBy = new BasicDBObject("$group", groupFields);
stages.add(groupBy);
DBObject project = new BasicDBObject("_id", 0);
project.put("value", 1);
project.put("Date", "$_id");
stages.add(new BasicDBObject("$project", project));
DBObject sort = new BasicDBObject("$sort", new BasicDBObject("Date", 1));
stages.add(sort);
AggregationOutput output = collectionG.aggregate(stages);
Now I want value 0 for the date that is not available in the collections that I have,
For example consider 2014-11-21 in the following :
[ { "value" : 6.0 , "Date" : { "$date" : "2014-11-19T04:00:00.000Z"}} , { "value" : 20.0 , "Date" : { "$date" : "2014-11-20T04:00:00.000Z"}},{ "value" : 0 , "Date" : { "$date" : "2014-11-21T04:00:00.000Z"}}]
instead of :
[ { "value" : 6.0 , "Date" : { "$date" : "2014-11-19T04:00:00.000Z"}} , { "value" : 20.0 , "Date" : { "$date" : "2014-11-20T04:00:00.000Z"}}}]
Is it possible to do that?
Why is checking in Java not reasonable? Setting average to 0 for 'nothing' is reasonable?
Depending on the context of your problem, one solution is for you to insert dummy records with 0 sentiment.

Extjs 4 How to merge 2 similar records into one record

I have Grid, where I want to merge similar records into one record.
I don't want to Group record, I want to Merge record
I mean,
{
{
firstField: "record1",
commonField: "ABC",
fourthField: "4"
},
{
firstField: "record2",
commonField: "ABC",
fourthField: "5"
},
{
firstField: "record3",
commonField: "ABC",
fourthField: "6"
}
}
So this the above JSON u can see there is "commonField" which has similar text.
So I have to show 1 record instead of 3 records, with FourthField added [4+5+6] so my final JSON should become like
{
{
firstField: "record1",
commonField: "ABC",
fourthField: "15"
}
}
Is there any way to achieve this ? like summary we do addition or some operation
similar can we do on Records ?