MongoDB doesnt give the right result - json

Problem with mongoDB
I have a collection in my mongodb with the following colums:
{"_id" : ObjectId("5443cddc425e215c7290a179"), "orderid" : 1002854, "customerid" : 45978, "campaignid" : 2141, "orderdate" : "2009-10-13 00:00:00", "city" : "NEWTON", "state" : "MA", "zipcode" : 2459, "paymenttype" : "VI", "totalprice" : 190, "numorderlines" : 3, "numunits" : 3}
There are 239 different campaignid's, which all should have a value of 1, but they all have 0.
Now i want to have per campaignid the total amount of numunits grouped, which i use the following statement:
db.orders.aggregate({$group: {_id: "$campaignid", Numunits: {$sum:" $numunits"}}})
It gives back the following result:
{ "_id" : 2146, "Numunits" : 0} , { "_id" : 2111, "Numunits" : 0} etc...
Now the expected result is:
{ "_id": 2146, "Numunits" : 93} , { "_id" : 2111, "Numunits" : 23} etc..

Mongo doesn't trim space for you if any. Try this:
db.orders.aggregate(
[
{$group: {
_id: "$campaignid",
Numunits: {$sum:"$numunits"}
}
}
])

Related

Jmeter: Extracting JSON response with special/spaces characters

Hello can someone help me extract the value of user parameter which is "testuser1"
I tried to use this JSON Path expression $..data I was able to extract the entire response but unable to extract user parameter. Thanks in advance
{
"data": "{ "took" : 13, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "bushidodb_history_network_eval_ea9656ef-0a9b-474b-8026-2f83e2eb9df1_2021-april-10", "_type" : "network", "_id" : "6e2e58be-0ccf-3fb4-8239-1d4f2af322e21618059082000", "_score" : 1.0, "_source" : { "misMatches" : [ "protocol", "state", "command" ], "instance" : "e3032804-4b6d-3735-ac22-c827950395b4|0.0.0.0|10.179.155.155|53|UDP", "protocol" : "UDP", "localAddress" : "0.0.0.0", "localPort" : "12345", "foreignAddress" : "10.179.155.155", "foreignPort" : "53", "command" : "ping yahoo.com ", "user" : "testuser1", "pid" : "10060", "state" : "OUTGOINGFQ", "rate" : 216.0, "originalLocalAddress" : "192.168.100.229", "exe" : "/bin/ping", "md5" : "f9ad63ce8592af407a7be43b7d5de075", "dir" : "", "agentId" : "abcd-dcd123", "year" : "2021", "month" : "APRIL", "day" : "10", "hour" : "12", "time" : "1618059082000", "isMerged" : false, "timestamp" : "Apr 10, 2021 12:51:22 PM", "metricKey" : "6e2e58be-0ccf-3fb4-8239-1d4f2af322e2", "isCompliant" : false }, "sort" : [ 1618059082000 ] } ] }, "aggregations" : { "count_over_time" : { "buckets" : [ { "key_as_string" : "2021-04-10T08:00:00.000-0400", "key" : 1618056000000, "doc_count" : 1 } ] } }}",
"success": true,
"message": {
"code": "S",
"message": "Get Eval results Count Success"
}
}
Actual Response:
Images
What you posted doesn't look like a valid JSON to me.
If in reality you're getting what's in your image, to wit:
{
"data": "{ \"took\" : 13, \"timed_out\" : false, \"_shards\" : { \"total\" : 5, \"successful\" : 5, \"skipped\" : 0, \"failed\" : 0 }, \"hits\" : { \"total\" : 1, \"max_score\" : 1.0, \"hits\" : [ { \"_index\" : \"bushidodb_history_network_eval_ea9656ef-0a9b-474b-8026-2f83e2eb9df1_2021-april-10\", \"_type\" : \"network\", \"_id\" : \"6e2e58be-0ccf-3fb4-8239-1d4f2af322e21618059082000\", \"_score\" : 1.0, \"_source\" : { \"misMatches\" : [ \"protocol\", \"state\", \"command\" ], \"instance\" : \"e3032804-4b6d-3735-ac22-c827950395b4|0.0.0.0|10.179.155.155|53|UDP\", \"protocol\" : \"UDP\", \"localAddress\" : \"0.0.0.0\", \"localPort\" : \"12345\", \"foreignAddress\" : \"10.179.155.155\", \"foreignPort\" : \"53\", \"command\" : \"pingyahoo.com\", \"user\" : \"testuser1\", \"pid\" : \"10060\", \"state\" : \"OUTGOINGFQ\", \"rate\" : 216.0, \"originalLocalAddress\" : \"192.168.100.229\", \"exe\" : \"/bin/ping\", \"md5\" : \"f9ad63ce8592af407a7be43b7d5de075\", \"dir\" : \"\", \"agentId\" : \"abcd-dcd123\", \"year\" : \"2021\", \"month\" : \"APRIL\", \"day\" : \"10\", \"hour\" : \"12\", \"time\" : \"1618059082000\", \"isMerged\" : false, \"timestamp\" : \"Apr10, 202112: 51: 22PM\", \"metricKey\" : \"6e2e58be-0ccf-3fb4-8239-1d4f2af322e2\", \"isCompliant\" : false }, \"sort\" : [ 1618059082000 ] } ] }, \"aggregations\" : { \"count_over_time\" : { \"buckets\" : [ { \"key_as_string\" : \"2021-04-10T08: 00: 00.000-0400\", \"key\" : 1618056000000, \"doc_count\" : 1 } ] } }}",
"success": true,
"message": {
"code": "S",
"message": "Get Eval results Count Success"
}
}
the easiest way is just using 2 JSON Extractors:
Extract data attribute value into a JMeter Variable from the response
Extract user attribute value into a JMeter variable from ${data} JMeter Variable:
Demo:
If the response looks like exactly you posted you won't be able to use JSON Extractors and will have to treat it as normal text so your choice is limited to Regular Expression Extractor, example regular expression:
"user"\s*:\s*"(\w+)"
Add Regular Expression extractor with the corresponding request and extract it. Use the below expression.
Expression: "user" : "(.*?)"
Ref: https://jmeter.apache.org/usermanual/regular_expressions.html
Regular Expression Extractor Sample

How to reformat specific data from json with jq

I'm new to json and want to extract data (blocks) from a specific json. I've tried to find information on how to do this with jq but so far I cannot seem to get what I want.
My json:
{
"now" : 1589987097.9,
"aircraft" : [
{
"mlat" : [],
"rssi" : -26.2,
"track" : 319,
"speed" : 354,
"messages" : 16,
"seen" : 0.7,
"altitude" : 38000,
"vert_rate" : 0,
"hex" : "44b5b4",
"tisb" : []
},
{
"squawk" : "6220",
"altitude" : 675,
"seen" : 1.1,
"messages" : 7220,
"tisb" : [],
"hex" : "484a95",
"mlat" : [],
"rssi" : -22
},
{
"hex" : "484846",
"tisb" : [],
"messages" : 20,
"speed" : 89,
"seen" : 0.4,
"squawk" : "7000",
"altitude" : 500,
"rssi" : -23.7,
"track" : 185,
"mlat" : []
},
{
"category" : "B1",
"mlat" : [],
"rssi" : -24.3,
"flight" : "ZSGBX ",
"altitude" : 3050,
"squawk" : "7000",
"seen" : 16.8,
"messages" : 37,
"tisb" : [],
"hex" : "00901a"
}
],
"messages" : 35857757
}
I would like to reformat this json to only include 'blocks' that contain specific hex values.
So for example, I want I would like my output to contain 44b5b4 and 00901a:
{
"now" : 1589987097.9,
"aircraft" : [
{
"mlat" : [],
"rssi" : -26.2,
"track" : 319,
"speed" : 354,
"messages" : 16,
"seen" : 0.7,
"altitude" : 38000,
"vert_rate" : 0,
"hex" : "44b5b4",
"tisb" : []
},
{
"category" : "B1",
"mlat" : [],
"rssi" : -24.3,
"flight" : "ZSGBX ",
"altitude" : 3050,
"squawk" : "7000",
"seen" : 16.8,
"messages" : 37,
"tisb" : [],
"hex" : "00901a"
}
],
"messages" : 35857757
}
Can someone tell me how to remove all items not having those 2 hex identifiers but still keep the same json structure?
Thanks a lot!
Do a select() on the array aircraft, matching only the required hex values. The map() function takes input the entire array and the result of the select operation i.e. filtering of objects based on the .hex value is updated back |= to the original array and the rest of the fields are kept intact.
jq '.aircraft |= map(select(.hex == "44b5b4" or .hex == "00901a"))' json
Select blocks whose hex matches one of the specific values and update aircraft to leave only those.
.aircraft |= map(select(.hex | IN("44b5b4", "00901a")))
Online demo

Create view with two object array from seperate collections in mongodb

I want to create a view which fields are come from two different object array with seperate collections.For more explanation here is my code
{
"_id" : ObjectId("5e4173cf51af9f555e64531c"),
"__v" : 0,
"createdDate" : ISODate("2020-02-24T08:33:24.100Z"),
"maxDate" : {
"month" : 12,
"year" : 2018
},
"modifiedDate" : ISODate("2020-02-24T08:33:33.025Z"),
"stockArray" : [
{
"purchase_bill_date" : "1/9/2017",
"purchase_bill_series" : "A",
"purchase_bill_item_no" : "419905",
"stock_code" : "5372123-ANN",
},
{
"purchase_bill_date" : "1/9/2017",
"purchase_bill_series" : "A",
"purchase_bill_item_no" : "419905",
"stock_name" : "53743N-10-10S",
}]
}
and the other collection
{
"_id" : ObjectId("5e4173cf51af9f555e64531c"),
"__v" : 0,
"yuklenimReports" : [
{
"purchase_bill_date" : "9/3/2018",
"purchase_bill_series" : null,
"purchase_bill_item_no" : "419905",
""stock_name" : "53743N-10-10S"
}]
}
i want to inner join purchase_bill_item_no and show the others.But i couldnt handle i spent too much hour to find the solution.Thanks for all help.

MongoDB query result is strange

I used Aggregation FrameWork of MongoDB. However, result was strange.
Some are arrayed json data and other is not arrayed json data.
The query is :
> db.employees.aggregate([ {$group : {_id:"$deptno", enames:{$addToSet : "$ename"}} },
{$sort:{_id:1}} ]).forEach(printjson)
The result is :
{ "_id" : 10, "enames" : [ "CLERK", "PRESIDENT", "CLARK" ] }
{
"_id" : 20,
"enames" : [
"FORD",
"ADAMS",
"JONES",
"SCOTT",
"SMITH"
]
}
{
"_id" : 30,
"enames" : [
"TURNER",
"BLAKE",
"WARD",
"MARTIN",
"JAMES",
"ALLEN"
]
}
{"_id" : 10} is not arrayed json data. However, {"_id" : 20} and {"_id" : 30} are arrayed json data.
I don't know what's wrong with it. Is forEach(printjson) wrong?

How to flatten sub-documents in mongo db

My collection has document like this
{
"_id" : ObjectId("587c8d0364b6e32706f7edef"),
"first_name" : "John",
"last_name" : "Doe",
"password" : "aasdjsabb12213b21bbcghc1h2",
"shift" : "A",
"dept" : "Management"
"Requests" : [
{
"weekId" : 1,
"MO" : 1,
"TU" : 2,
"W" : 3,
"TH" : 9,
"FR" : 10,
"SA" : 6,
"SU" : 1
}
]
}
I want to export the result of my query to csv and need fields flattened out like this
{
"_id" : ObjectId("587c8d0364b6e32706f7edef"),
"first_name" : "John",
"last_name" : "Doe",
"password" : "aasdjsabb12213b21bbcghc1h2",
"shift" : "A",
"dept" : "Management"
"weekId" : 1,
"MO" : 1,
"TU" : 2,
"W" : 3,
"TH" : 9,
"FR" : 10,
"SA" : 6,
"SU" : 1
}
I am trying to use aggregate function but to no avail. Can anyone suggest me how to do this?
This is my working code but I don't think this is the right way
db.req.aggregate([{$unwind:'$Requests'},{$project: {first_name:1,last_name:1,dept:1,"WeekId":"$Requests.weekdId","Mon":"$Requests.MO","Tue":"$Requests.TU","Wed":"$Requests.W","Thu":"$Requests.TH","Fri":"$Requests.FR","Sat":"$Requests.SA","Sun":"$Requests.SU"}},{$out:"results"}]);
You can do this with an aggregate query, but its not very pretty:
db.test.aggregate([
{$unwind:"$Requests"},
{$project:
{_id:1,
first_name:1,
last_name:1,
password:1,
shift:1,
dept:1,
weekId:"$Requests.weekId",
MO:"$Requests.MO",
TU:"$Requests.TU",
W:"$Requests.W",
TH:"$Requests.TH",
FR:"$Requests.FR",
SA:"$Requests.SA",
SU:"$Requests.SU"}}])
.pretty()
So basically unwind the Requests array, then project out the document you want to produce. Hope this makes sense.