Vega-Lite : Stacked Grouped Bar Chart - google-data-studio

I'm using Vega-Lite for Data Studio and I'm trying to create a Stacked AND Grouped bar chart.
For the moment, I have this result : https://i.stack.imgur.com/HO1wR.png
However, what I want to do is to have LEFT bars with GREEN gradient & RIGHT bars with BLUE gradient.
So my question is : How can I perform this kind of chart ?
e.g : https://i.stack.imgur.com/J5223.png
Here, you'll find what I've done :
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values" : [
{"Team" : "X", "Task" : "A", "Done" : 56.5, "Planned" : 80, "Activity_Type" : "TypeA"},
{"Team" : "X", "Task" : "A", "Done" : 26, "Planned" : 14, "Activity_Type" : "TypeB"},
{"Team" : "X", "Task" : "B", "Done" : 26, "Planned" : 21, "Activity_Type" : "TypeA"},
{"Team" : "X", "Task" : "B", "Done" : 16.5, "Planned" : 36, "Activity_Type" : "TypeB"},
{"Team" : "X", "Task" : "C", "Done" : 41.5, "Planned" : 59, "Activity_Type" : "TypeA"},
{"Team" : "X", "Task" : "C", "Done" : 9, "Planned" : 12, "Activity_Type" : "TypeB"}
]
},
"height" : 200,
"width" : 500,
"encoding": {
"tooltip" : [
{"field" : "Team"},
{"field" : "Task"},
{"field" : "Done"},
{"field" : "Planned"},
{"field" : "Activity_Type"}
],
"y": {"axis": {"title": "Number of points"}},
"x": {"field": "Task", "type": "nominal", "axis": {"labelAngle": 0}}
},
"layer": [
{
"mark": {"type": "bar", "xOffset": -16, "size": 30, "color": "#81c784"},
"encoding": {
"y": {
"field": "Done",
"type": "quantitative"
},
"color": {
"field": "Activity_Type",
"type": "nominal",
"scale": {
"range": ["#81c784", "#629b65", "#3d683f"] // GREEN GRADIENT
},
"title": "Activity_Type",
"legend" : null
}
}
},
{
"mark": {
"type": "bar", "size": 30, "xOffset": 16, "color" : "#1e88e5"
},
"encoding": {
"y": {
"field": "Planned",
"type": "quantitative"
},
"color": {
"field": "Activity_Type",
"type": "nominal",
"scale": {
"range": ["#1e88e5", "#2f75b3", "#255279"] // BLUE GRADIENT
},
"legend" : null
}
}
}
]
}
Thanks by advance for your help !

I've finally found a solution that does the job !
For people who wants, here's my solution :
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values" : [
{"Team" : "X", "Task" : "A", "Done" : 56.5, "Planned" : 80, "Activity_Type" : "TypeA"},
{"Team" : "X", "Task" : "A", "Done" : 26, "Planned" : 14, "Activity_Type" : "TypeB"},
{"Team" : "X", "Task" : "B", "Done" : 26, "Planned" : 21, "Activity_Type" : "TypeA"},
{"Team" : "X", "Task" : "B", "Done" : 16.5, "Planned" : 36, "Activity_Type" : "TypeB"},
{"Team" : "X", "Task" : "C", "Done" : 41.5, "Planned" : 59, "Activity_Type" : "TypeA"},
{"Team" : "X", "Task" : "C", "Done" : 9, "Planned" : 12, "Activity_Type" : "TypeB"}
]
},
"height" : 200,
"width" : 500,
"encoding": {
"tooltip" : [
{"field" : "Team"},
{"field" : "Task"},
{"field" : "Done"},
{"field" : "Planned"},
{"field" : "Activity_Type"}
],
"y": {"axis": {"title": "Number of points"}},
"x": {"field": "Task", "type": "nominal", "axis": {"labelAngle": 0}}
},
"layer": [
{
"mark": {"type": "bar", "xOffset": -16, "size": 30, "color": "#81c784"},
"encoding": {
"y": {
"field": "Done",
"type": "quantitative"
},
"color": {
"condition" : [
{"test" : "datum.Activity_Type === 'TypeA'", "value" : "#629b65"},
{"test" : "datum.Activity_Type === 'TypeB'", "value" : "#81c784"}
],
"value": "#3d683f"
}
}
},
{
"mark": {
"type": "bar", "size": 30, "xOffset": 16, "color" : "#1e88e5"
},
"encoding": {
"y": {
"field": "Planned",
"type": "quantitative"
},
"color": {
"condition" : [
{"test" : "datum.Activity_Type === 'TypeA'","value" : "#1e88e5"},
{"test" : "datum.Activity_Type === 'TypeB'","value" : "#2f75b3"}
],
"value": "#255279"
}
}
}
]
}
It looks like this:

Related

Filter ElasticSearch results based on AND condition in an array

Below is the data sample of a single record in my Index:
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "49605102905391763685971719283371021096086998966740189186",
"_score" : 2.8858113,
"_source" : {
"properties" : {
"activity" : [
{
"activityId" : 39,
"actionVersion" : 2,
"actionIdx" : 3
},
{
"activityId" : 39,
"actionVersion" : 2,
"actionIdx" : 4
},
{
"activityId" : 39,
"actionVersion" : 2,
"actionIdx" : 5
},
{
"activityId" : 42,
"actionVersion" : 2,
"actionIdx" : 3
},
{
"activityId" : 42,
"actionVersion" : 2,
"actionIdx" : 4
},
{
"activityId" : 42,
"actionVersion" : 2,
"actionIdx" : 5
}
]
}
}
}
I want to filter results based on activitiyId 42 and actionIdx 3. I'm using below query trying to get results.
{"size":-1,
"query": {
"bool": {
"should": {
"bool": {
"must": [
{
"match": {
"activityId": 42
}
},
{
"match": {
"actionIdx": 3
}
}
]
}
}
}
}
}
I'm getting the records that contain activityid 42 and actionidx 3 but my requirement is to have records that contain activity 42 and actionidx 3 in the same object. Below are my results with above search query:
"activity" : [
{
"activityId" : 39,
"actionVersion" : 2,
"actionIdx" : 2
},
{
"activityId" : 28,
"actionVersion" : 2,
"actionIdx" : 3
},
{
"activityId" : 42,
"actionVersion" : 2,
"actionIdx" : 2
},
{
"activityId" : 41,
"actionVersion" : 2,
"actionIdx" : 3
}
]
My expected output to be:
"activity" : [
{
"activityId" : 39,
"actionVersion" : 2,
"actionIdx" : 2
},
{
"activityId" : 28,
"actionVersion" : 2,
"actionIdx" : 3
},
{
"activityId" : 42,
"actionVersion" : 2,
"actionIdx" : 3
}
]
Here's is the mapping index. Activity is not nested
"properties" : {
"properties" : {
"activity" : {
"properties" : {
"actionIdx" : {
"type" : "long"
},
"actionVersion" : {
"type" : "long"
},
"activityId" : {
"type" : "long"
}
}
}
}
}
you need to use nested data type and query to achieve that, right now as you are not using it, its considered as object data type(default) and nested documents clearly mention the issue you are facing.
When ingesting key-value pairs with a large, arbitrary set of keys,
you might consider modeling each key-value pair as its own nested
document with key and value fields. Instead, consider using the
flattened data type, which maps an entire object as a single field and
allows for simple searches over its contents. Nested documents and
queries are typically expensive, so using the flattened data type for
this use case is a better option.
Index Mapping
{
"mappings": {
"properties": {
"properties": {
"properties": {
"activity": {
"type": "nested"
}
}
}
}
}
}
Index Data:
{
"properties": {
"activity": [
{
"activityId": 39,
"actionVersion": 2,
"actionIdx": 3
},
{
"activityId": 39,
"actionVersion": 2,
"actionIdx": 4
},
{
"activityId": 39,
"actionVersion": 2,
"actionIdx": 5
},
{
"activityId": 42,
"actionVersion": 2,
"actionIdx": 3
},
{
"activityId": 42,
"actionVersion": 2,
"actionIdx": 4
},
{
"activityId": 42,
"actionVersion": 2,
"actionIdx": 5
}
]
}
}
Search Query:
{
"query": {
"nested": {
"path": "properties.activity",
"query": {
"bool": {
"must": [
{
"match": {
"properties.activity.activityId": 42
}
},
{
"match": {
"properties.activity.actionIdx": 3
}
}
]
}
},
"inner_hits":{}
}
}
}
Search Result
"hits": [
{
"_index": "fd_cb1",
"_type": "_doc",
"_id": "1",
"_nested": {
"field": "properties.activity",
"offset": 3
},
"_score": 2.0,
"_source": {
"activityId": 42,
"actionVersion": 2,
"actionIdx": 3
}
}
]

Elasticsearch : How to group documents by field when field is an array

My Elasticsearch index looks like that:
{
"team": ["Jane","Jason"],
"date": "2020/07/23 12:00:56",
"is_work_done": true
},
{
"team": ["Jane","Jason"],
"date": "2020/07/22 14:23:56",
"is_work_done": false
},
{
"team": ["Jane","Jason","Anna"],
"date": "2020/07/17 09:22:10",
"is_work_done": false
},
{
"team": ["Alex","George","Anna"],
"date": "2020/07/13 03:24:19",
"is_work_done": true
}
My mapping is:
{
"mappings": {
"type_name": {
"properties": {
"team": { "type": "keyword" },
"date": {"type": "date", "format": "yyyy/MM/dd HH:mm:ss"},
"is_work_done": { "type": "boolean" }
}
}
}
}
I would like to collect information on each team. How can I group my documents by team?
I created this index to solve this issue because in reality, I don't know how many members each team contain.
I tried to aggregate the documents but I don't find which type of aggregation would fit.
For example, with this query:
GET /testbench-test/_search
{
"aggs": {
"mybucket": {
"terms": { "field": "team" }
}
}
}
I get this result:
"aggregations" : {
"mybucket" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "Jane",
"doc_count" : 3
},
{
"key" : "Jason",
"doc_count" : 3
},
{
"key" : "Anna",
"doc_count" : 2
},
{
"key" : "Alex",
"doc_count" : 1
},
{
"key" : "George",
"doc_count" : 1
}
]
}
}
Thanks for helping!
Edit:
With the query on my real index which contains 64,030 indexations:
POST _search
{
"aggs": {
"teams": {
"terms": {
"script": "doc['team'].join(' & ')",
"size": 10
}
}
}
}
I get this result:
{
"took" : 52,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 64031,
"max_score" : 1.0,
"hits" : [
{
"_index" : "test-logs",
"_type" : "log",
"_id" : "98",
"_score" : 1.0,
"_source" : {
"uuid" : "9827af",
"benchId" : "m",
"benchGroup" : "e",
"machine" : "CH",
"sha1" : "ddf380fd6a2f930813",
"date" : "2019/04/25 11:40:19",
"duration" : 0.9953742847,
"isCss" : false,
"isPassed" : true,
"finalStatus" : "OK_VNA_TEST",
"team" : [
"MCANT00013A9D00002"
]
}
},
{
"_index" : "test-logs",
"_type" : "log",
"_id" : "b0c9a8aa4",
"_score" : 1.0,
"_source" : {
"uuid" : "b0c9a1be0a8aa4",
"benchId" : "m",
"benchGroup" : "e",
"machine" : "CH",
"sha1" : "ddf385a1562d78813",
"date" : "2019/04/29 08:08:37",
"duration" : 0.4976871423,
"isCss" : false,
"isPassed" : true,
"finalStatus" : "OK_VNA_TEST",
"team" : [
"MCANT00009A9D00149"
]
}
},
{
"_index" : "test-logs",
"_type" : "log",
"_id" : "bb1d525e2a368f6d4",
"_score" : 1.0,
"_source" : {
"uuid" : "bb1da368f6d4",
"benchId" : "m",
"benchGroup" : "e",
"machine" : "CH",
"sha1" : "ddf380fd6a2f85a1562d78813",
"date" : "2019/04/29 08:09:51",
"duration" : 0.5208305083,
"isCss" : false,
"isPassed" : true,
"finalStatus" : "OK_VNA_TEST",
"team" : [
"MCANT00009A9D00179"
]
}
},
{
"_index" : "test-logs",
"_type" : "log",
"_id" : "87bec43ce2553c590b",
"_score" : 1.0,
"_source" : {
"uuid" : "87bec43c-e2553c590b",
"benchId" : "m",
"benchGroup" : "e",
"machine" : "CH",
"sha1" : "ddf380fd6a2f9302bcdf8c26e1f85a1562d78813",
"date" : "2019/04/29 08:10:10",
"duration" : 0.4629604518,
"isCss" : false,
"isPassed" : true,
"finalStatus" : "OK_VNA_TEST",
"team" : [
"MCANT00009A9D00181"
]
}
},
{
"_index" : "test-logs",
"_type" : "log",
"_id" : "3499224bdac4fa39",
"_score" : 1.0,
"_source" : {
"uuid" : "349922444bdac4fa39",
"benchId" : "m",
"benchGroup" : "e",
"machine" : "CH",
"sha1" : "ddf380fd6e1f85a1562d78813",
"date" : "2019/04/29 08:10:49",
"duration" : 0.5092588253,
"isCss" : false,
"isPassed" : true,
"finalStatus" : "OK_VNA_TEST",
"uut" : [
"MCANT00009A9D00171"
]
}
},
{
"_index" : "test-logs",
"_type" : "log",
"_id" : "f8236de0-dd6a97b7a81",
"_score" : 1.0,
"_source" : {
"uuid" : "f8236de0-add6a97b7a81",
"benchId" : "m",
"benchGroup" : "e",
"machine" : "CH",
"sha1" : "ddf380fd6a2f93085a1562d78813",
"date" : "2019/04/29 09:51:47",
"duration" : 0.6134272553,
"isCss" : false,
"isPassed" : true,
"finalStatus" : "OK_VNA_TEST",
"team" : [
"MCANT00009A9D00221"
]
}
},
{
"_index" : "test-logs",
"_type" : "log",
"_id" : "339fa5a2e-cf9f1f4738bf",
"_score" : 1.0,
"_source" : {
"uuid" : "339fa5a9f1f4738bf",
"benchId" : "m",
"benchGroup" : "e",
"machine" : "CH",
"sha1" : "ddf380fd6a2f932d78813",
"date" : "2019/04/29 09:51:57",
"duration" : 0.6249989383,
"isCss" : false,
"isPassed" : true,
"finalStatus" : "OK_VNA_TEST",
"team" : [
"MCANT00009A9D00185"
]
}
},
{
"_index" : "test-logs",
"_type" : "log",
"_id" : "dbd45ec4-f53b-4bda-9eeb-dadf2e3ab366",
"_score" : 1.0,
"_source" : {
"uuid" : "dbd45ec4-f53b-4bda-9eeb-dadf2e3ab366",
"benchId" : "m",
"benchGroup" : "e",
"machine" : "CH",
"sha1" : "ddf380fd6a2f9302bcdfd78813",
"date" : "2019/04/29 09:52:19",
"duration" : 0.5787005648,
"isCss" : false,
"isPassed" : true,
"finalStatus" : "OK_VNA_TEST",
"team" : [
"MCANT00009A9D00184"
]
}
},
{
"_index" : "test-logs",
"_type" : "log",
"_id" : "bc0d3548bed68",
"_score" : 1.0,
"_source" : {
"uuid" : "bc0d354348bed68",
"benchId" : "m",
"benchGroup" : "e",
"machine" : "CH",
"sha1" : "ddf3801562d78813",
"date" : "2019/04/29 08:08:12",
"duration" : 0.5208305083,
"isCss" : false,
"isPassed" : true,
"finalStatus" : "OK_VNA_TEST",
"team" : [
"MCANT00009A9D00160"
]
}
},
{
"_index" : "test-logs",
"_type" : "log",
"_id" : "d591b9e7-683a-4d78-be31-1b137b8a3b2b",
"_score" : 1.0,
"_source" : {
"uuid" : "d591b9e7-683a-4d78-be31-1b137b8a3b2b",
"benchId" : "m",
"benchGroup" : "e",
"machine" : "CH",
"sha1" : "ddf380fd6a2f93013",
"date" : "2019/04/29 08:08:05",
"duration" : 0.6828689948,
"isCss" : false,
"isPassed" : true,
"finalStatus" : "OK_VNA_TEST",
"team" : [
"MCANT00009A9D00146"
]
}
}
]
},
"aggregations" : {
"teams" : {
"doc_count_error_upper_bound" : 25,
"sum_other_doc_count" : 63658,
"buckets" : [
{
"key" : "SI050010AA8C00001",
"doc_count" : 86
},
{
"key" : "00845D9E0137 & 00851F9E0095 & CPPCB00608C9F00060 & MCSAS00676A9F00141_-27.8_B & MCSAS00677A9F00146_23.0_B & SI050012AG9F00060",
"doc_count" : 56
},
{
"key" : "00845D9E0275 & 00851F9G0056 & CPPCB00608C9F00127 & MCSAS00676B9G00012 & MCSAS00676B9G00012_-21.2_C & MCSAS00677B9G00005 & MCSAS00677B9G00005_32.5_C & SI050012AG9F00127",
"doc_count" : 43
},
{
"key" : "00844G9D0041 & 00847D9G0020 & MCANT00009A9G00048 & MCSAS00652F9E00091 & S20-17272 & SI050001AG9G00055 & SI050004AA9F00059",
"doc_count" : 39
},
{
"key" : "00845D9E0035 & 00851F9E0062 & CPPCB00608C9E00034 & MCSAS00676A9E00090_-35.0_B & MCSAS00677A9E00089_31.0_B & SI050012AG9E00034",
"doc_count" : 34
},
{
"key" : "IX & IX-c2-67063 & IX-x2-00511 & SI050010AA9A00002 & droneProduction",
"doc_count" : 27
},
{
"key" : "IX & IX-12-10251 & IX-x2-00484 & SI050001AF9A00020 & SI050010AA8J00154 & SI050012AG9D00082 & droneClient",
"doc_count" : 25
},
{
"key" : "MCANT00009A9G00048 & MCSAS00652F9E00091 & S20-17272 & SI050001AG9G00055",
"doc_count" : 24
},
{
"key" : "00883C0F0000",
"doc_count" : 20
},
{
"key" : "00844C8B0029 & 00847C8E0018 & 00849A8B0015 & MCANT00009A8E00017 & N/A & S20-00533 & SI050002AA8E000001 & SI050004AA8E000514",
"doc_count" : 19
}
]
}
}
}
Why not all the different 'teams' have their own bucket?
What you can do is to use a script in your terms aggregation like this:
POST teams/_search
{
"size": 0,
"aggs": {
"teams": {
"terms": {
"script": "doc['team'].join('-')",
"size": 10
}
}
}
}
And the result you're going to get is something like this:
"buckets" : [
{
"key" : "Jane-Jason",
"doc_count" : 2
},
{
"key" : "Alex-Anna-George",
"doc_count" : 1
},
{
"key" : "Anna-Jane-Jason",
"doc_count" : 1
}
]

Checking $gt on a structure element in MongoDB [duplicate]

This question already has answers here:
How to query nested objects?
(3 answers)
Closed 2 years ago.
> db.inventory.find()
{ "_id" : ObjectId("5eb67598bee5213484d45087"), "item" : "journal", "qty" : 25, "status" : "A", "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
{ "_id" : ObjectId("5eb67598bee5213484d45088"), "item" : "notebook", "qty" : 50, "status" : "A", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank" ] }
{ "_id" : ObjectId("5eb67598bee5213484d45089"), "item" : "paper", "qty" : 10, "status" : "D", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank", "plain" ] }
{ "_id" : ObjectId("5eb67598bee5213484d4508a"), "item" : "planner", "qty" : 0, "status" : "D", "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
{ "_id" : ObjectId("5eb67598bee5213484d4508b"), "item" : "postcard", "qty" : 45, "status" : "A", "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "tags" : [ "blue" ] }
{ "_id" : ObjectId("5ebfd02b3a3b38a52be04608"), "item" : "journal", "qty" : 25, "tags" : [ "blank", "red" ], "dim_cm" : [ 14, 21 ] }
{ "_id" : ObjectId("5ebfd02b3a3b38a52be04609"), "item" : "notebook", "qty" : 50, "tags" : [ "red", "blank" ], "dim_cm" : [ 14, 21 ] }
{ "_id" : ObjectId("5ebfd02b3a3b38a52be0460a"), "item" : "paper", "qty" : 100, "tags" : [ "red", "blank", "plain" ], "dim_cm" : [ 14, 21 ] }
{ "_id" : ObjectId("5ebfd02b3a3b38a52be0460b"), "item" : "planner", "qty" : 75, "tags" : [ "blank", "red" ], "dim_cm" : [ 22.85, 30 ] }
{ "_id" : ObjectId("5ebfd02b3a3b38a52be0460c"), "item" : "postcard", "qty" : 45, "tags" : [ "blue" ], "dim_cm" : [ 10, 15.25 ] }
Both of the following queries are resulting in nothing:
> db.inventory.find( { size: { h: { $gt: 14 } } } )
>
> db.inventory.find( { size: { $elemMatch: { h: { $gt: 14 } } } } )
Whereas there is a document greater than 14:
{ "_id" : ObjectId("5eb67598bee5213484d4508a"), "item" : "planner", "qty" : 0, "status" : "D", "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
Following syntax doesn't work:
> db.inventory.find( { size.h: { $gt: 14 } } )
2020-05-24T15:08:44.306+0530 E QUERY [js] uncaught exception: SyntaxError: missing : after property id :#(shell):1:25
`
Please explain why.
You need to add dot notation while querying nested documents. Can you try this
db.inventory.find( { 'size.h': { $gt: 14 } } )

Unable to find a match in MongoDB query

> db.inventory.find( )
{ "_id" : ObjectId("5eb67598bee5213484d45087"), "item" : "journal", "qty" : 25, "status" : "A", "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
{ "_id" : ObjectId("5eb67598bee5213484d45088"), "item" : "notebook", "qty" : 50, "status" : "A", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank" ] }
{ "_id" : ObjectId("5eb67598bee5213484d45089"), "item" : "paper", "qty" : 10, "status" : "D", "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "tags" : [ "red", "blank", "plain" ] }
{ "_id" : ObjectId("5eb67598bee5213484d4508a"), "item" : "planner", "qty" : 0, "status" : "D", "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "tags" : [ "blank", "red" ] }
{ "_id" : ObjectId("5eb67598bee5213484d4508b"), "item" : "postcard", "qty" : 45, "status" : "A", "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "tags" : [ "blue" ] }
{ "_id" : ObjectId("5ebfd02b3a3b38a52be04608"), "item" : "journal", "qty" : 25, "tags" : [ "blank", "red" ], "dim_cm" : [ 14, 21 ] }
{ "_id" : ObjectId("5ebfd02b3a3b38a52be04609"), "item" : "notebook", "qty" : 50, "tags" : [ "red", "blank" ], "dim_cm" : [ 14, 21 ] }
{ "_id" : ObjectId("5ebfd02b3a3b38a52be0460a"), "item" : "paper", "qty" : 100, "tags" : [ "red", "blank", "plain" ], "dim_cm" : [ 14, 21 ] }
{ "_id" : ObjectId("5ebfd02b3a3b38a52be0460b"), "item" : "planner", "qty" : 75, "tags" : [ "blank", "red" ], "dim_cm" : [ 22.85, 30 ] }
{ "_id" : ObjectId("5ebfd02b3a3b38a52be0460c"), "item" : "postcard", "qty" : 45, "tags" : [ "blue" ], "dim_cm" : [ 10, 15.25 ] }
>
> db.inventory.find( { item: 'journal', qty: '25' } )
>
item: 'journal' with qty '25' is the first item. Why isn't it being shown?
Here is a similar query which is working as per them: https://www.w3resource.com/mongodb-exercises/mongodb-exercise-22.php
db.restaurants.find(
{
"grades.date": ISODate("2014-08-11T00:00:00Z"),
"grades.grade":"A" ,
"grades.score" : 11
},
{"restaurant_id" : 1,"name":1,"grades":1}
);
qty is a number and you're trying to find a string, try:
db.inventory.find( { item: 'journal', qty: 25 } )
(docs)
For most data types, however, comparison operators only perform comparisons on documents where the BSON type of the target field matches the type of the query operand.

Deleting Object from Nested Array MongoDB

I am trying to delete the object with the id: "2019-08-22T04:53:11.357Z" from this users portfolio array.
I have searched for the correct query filter to help me delete the object but nothing seems to be working!
Here is the data we are working with. There is currently only one user
(InStock) in the system but more will be added in the future.
[
{
"id": "MTg4MDU3ODM2MDk2NDU0NjU3",
"name": "InStock",
"balance": 7760,
"portfolio": [
{
"id": "2019-08-22T04:15:22.998Z",
"name": "Jordan Shoe",
"size": "10.5",
"price": 150
},
{
"id": "2019-08-22T04:36:37.836Z",
"name": "Nike Tee",
"size": "M",
"price": 35
},
{
"id": "2019-08-22T04:53:11.357Z",
"name": "Adidas Shoe",
"size": "8.5",
"price": 100
}
],
"history": [
]
}
]
and here is what I was trying using what I've seen in other solutions.
db.collection(collectionName).updateOne({"id": "MTg4MDU3ODM2MDk2NDU0NjU3"}, {$pull : {"portfolio": {"id": "2019-08-22T04:36:37.836Z"}}})
I am looking for the line to remove the Adidas Shoe object from InStock's portfolio array.
Try this query:
db.test1.updateOne({"_id" : ObjectId("5d5e7a291b761bfc0420e580")},
{$pull: {"portfolio": {"name": "Adidas Shoe"}}} )
After execution:
{
"_id" : ObjectId("5d5e7a291b761bfc0420e580"),
"portfolio" : [
{
"id" : "2019-08-22T04:15:22.998Z",
"name" : "Jordan Shoe",
"size" : "10.5",
"price" : 150.0
},
{
"id" : "2019-08-22T04:36:37.836Z",
"name" : "Nike Tee",
"size" : "M",
"price" : 35.0
}
],
"name" : "InStock",
"balance" : 7760.0
}
It's Working Fine for you.
db.collection(collectionName).updateOne({"id": "MTg4MDU3ODM2MDk2NDU0NjU3"},
{ $pull: { portfolio: { $elemMatch: { id:"2019-08-22T04:36:37.836Z"} }},

Resources