How update string to number field - mongodb

/* 1 */
{
"_id" : ObjectId("62622dd73905f04f59db2971"),
"array1" : [
{
"_id" : "21",
"array2" : [
{
"_id" : "123",
"answeredBy" : [
"success"
]
},
{
"_id" : "124",
"answeredBy" : []
}
]
}
]
}
/* 2 */
{
"_id" : ObjectId("626230e03905f04f59db29f5"),
"array1" : [
{
"_id" : "22",
"array2" : [
{
"_id" : "223",
"answeredBy" : []
},
{
"_id" : "220",
"answeredBy" : []
}
]
}
]
}
How to convert
"_id" : "21", and "_id" : "22",
to
"_id" : 21, and "_id" : 22,

Here's one way to do it.
db.collection.update({
"array1._id": { "$exists": true }
},
[
{
"$set": {
"array1": {
"$map": {
"input": "$array1",
"as": "elem",
"in": {
"$mergeObjects": [
"$$elem",
{ "_id": { "$toInt": "$$elem._id" } }
]
}
}
}
}
}
],
{
"multi": true
})
Try it on mongoplayground.net.

Related

MongoDB Query that will $project document values in horizontal format

Im trying to create a query that will project or show the field in HORIZONTAL format. Below is my sample collection and the expected output. BTW, Im also using jaspersoft studio to create the report but i think my output should be done in mongodb query. Hope someone can help. Thanks!
"_id" : ObjectId("60ddc6f44c893c117141e9b9"),
"observationvalues" : [
{
"_id" : ObjectId("60ddc6f44c893c117141e9c3"),
"name" : "Systolic BP",
"resultvalue" : "88.00"
},
{
"_id" : ObjectId("60ddc6f44c893c117141e9c2"),
"name" : "Diastolic BP",
"resultvalue" : "66.00"
},
{
"_id" : ObjectId("60ddc6f44c893c117141e9c1"),
"name" : "Weight",
"resultvalue" : "90.00"
},
{
"_id" : ObjectId("60ddc6f44c893c117141e9c5"),
"name" : "Height",
"resultvalue" : null
}
],
"createdat" : ISODate("2021-07-01T13:45:24.679Z"),
"modifiedat" : ISODate("2021-07-01T13:45:24.679Z"),
"statusflag" : "A"
"_id" : ObjectId("60ddc6f44c893c117141e8b8"),
"observationvalues" : [
{
"_id" : ObjectId("60ddc6f44c893c117141e9b3"),
"name" : "Systolic BP",
"resultvalue" : "84.00"
},
{
"_id" : ObjectId("60ddc6f44c893c117141e9b2"),
"name" : "Diastolic BP",
"resultvalue" : "63.00"
},
{
"_id" : ObjectId("60ddc6f44c893c117141e9b1"),
"name" : "Weight",
"resultvalue" : "99.00"
},
{
"_id" : ObjectId("60ddc6f44c893c117141e9b5"),
"name" : "Height",
"resultvalue" : 172.00
}
],
"createdat" : ISODate("2021-07-02T13:45:24.679Z"),
"modifiedat" : ISODate("2021-07-02T13:45:24.679Z"),
"statusflag" : "A"
"_id" : ObjectId("60ddc6f44c893c117141e7b7"),
"observationvalues" : [
{
"_id" : ObjectId("60ddc6f44c893c117141e9a3"),
"name" : "Systolic BP",
"resultvalue" : "81.00"
},
{
"_id" : ObjectId("60ddc6f44c893c117141e9a2"),
"name" : "Diastolic BP",
"resultvalue" : "65.00"
},
{
"_id" : ObjectId("60ddc6f44c893c117141e9a1"),
"name" : "Weight",
"resultvalue" : "96.00"
},
{
"_id" : ObjectId("60ddc6f44c893c117141e9a5"),
"name" : "Height",
"resultvalue" : 165.00
}
],
"createdat" : ISODate("2021-07-03T13:45:24.679Z"),
"modifiedat" : ISODate("2021-07-03T13:45:24.679Z"),
"statusflag" : "A",
The expected Output will be:
{"createdat" : "2021-07-01T13:45:24.679Z", "Systolic BP" : 88.00 ,"Diastolic BP" : 66.00 ,"Weight": 90.00 ,"Height":null }
{"createdat" : "2021-07-01T13:45:24.679Z", "Systolic BP" : 84.00 ,"Diastolic BP" : 63.00 ,"Weight": 99.00 ,"Height":172.00 }
{"createdat" : "2021-07-03T13:45:24.679Z", "Systolic BP" : 81.00 ,"Diastolic BP" : 65.00 ,"Weight": 96.00 ,"Height":165.00 }
I have created my query but it is not having the output in one line grouped for each createdat field
{
$project : {
_id: {
"systolic": {"$cond": [ { "$eq": ["$name","Systolic BP" ] }, "$resultvalue","" ] },
"diastolic": {"$cond": [ { "$eq": ["$name","Diastolic BP" ] }, "$resultvalue","" ] },
"weight": {"$cond": [ { "$eq": ["$name","Weight" ] }, "$resultvalue","" ] },
"height": {"$cond": [ { "$eq": ["$name","Height" ] }, "$resultvalue","" ] },
"create": "$createdat"
}
}
},
Your expected data output could be done like this: playground
[
{
"$project": {
"createdat": 1,
"rows": {
"$arrayToObject": {
"$map": {
"input": "$observationvalues",
"in": {
"k": "$$this.name",
"v": {
"$convert": {
"input": "$$this.resultvalue",
"to": "double",
"onError": null,
"onNull": null
}
}
}
}
}
}
}
},
{
"$addFields": {
"rows.create": "$createdat"
}
},
{
"$replaceRoot": {
"newRoot": "$rows"
}
},
{
"$match": {
"Systolic BP": 84.00
}
}
]
To get them in one line you should look for some JSON formater.

how to sort an array in a nested array which is located under an object in mongodb

I have a collection data like below.
{
"name": "Devices",
"exten": {
"parameters": [{
"name": "Date",
"value": ["5","2"]
}, {
"name": "Time",
"value": ["2"]
}, {
"name": "Season",
"value": ["6"]
}
]
}
}
I want to take all data which is name "Devices" and sort by first index of "Value" which is parameter name is "Date"
ex: mongo will get
name = "devices"
exten.parameters.name = "Date"
will sort it by
exten.parameters.value[0]
in this example it will be sorted by "5".
below query returns 0 record.
db.brand.aggregate(
{ $match: {
"name" : "Devices"
}},
{ $unwind: "$exten.parameters" },
{ $match: {
'exten.parameters.name': 'Date'
}},
{ $sort: {
'exten.parameters.value': -1
}}
)
The following query can get us the expected output:
db.collection.aggregate([
{
$match:{
"name":"Devices"
}
},
{
$unwind:"$exten.parameters"
},
{
$match:{
"exten.parameters.name":"Date"
}
},
{
$project:{
"name":1,
"exten":1,
"firstParam":{
$arrayElemAt:["$exten.parameters.value",0]
}
}
},
{
$sort:{
"firstParam":1
}
},
{
$project:{
"firstParam":0
}
}
]).pretty()
Data set:
{
"_id" : ObjectId("5da02fb86472ba670fd8c159"),
"name" : "Devices",
"exten" : {
"parameters" : [
{
"name" : "Date",
"value" : [
"5",
"2"
]
},
{
"name" : "Date",
"value" : [
"2",
"7"
]
},
{
"name" : "Time",
"value" : [
"2"
]
},
{
"name" : "Season",
"value" : [
"6"
]
}
]
}
}
Output:
{
"_id" : ObjectId("5da02fb86472ba670fd8c159"),
"name" : "Devices",
"exten" : {
"parameters" : {
"name" : "Date",
"value" : [
"2",
"7"
]
}
}
}
{
"_id" : ObjectId("5da02fb86472ba670fd8c159"),
"name" : "Devices",
"exten" : {
"parameters" : {
"name" : "Date",
"value" : [
"5",
"2"
]
}
}
}

Mongo Aggregation to replace an empty / null array field inside an array with a default array

I have a collection like below
{
relatedProperties: [ //Array
{
locations: [ //Array
{
value: "Brazil"
},
{
value: "Germany"
}
]
},
{
locations: []
},
{
locations: null
}
]
}
How do I write an aggregation to make only the empty or null arrays to have a default value like;
locations: [
{
value: "India"
}
]
You can use $mergeObjects to keep other fields whatever they are:
db.collection.aggregate([
{
$project: {
relatedProperties: {
$map: {
input: "$relatedProperties",
as: "rp",
in: {
$cond: {
if: {
$eq: [
{
$ifNull: [
"$$rp.locations",
[]
]
},
[]
]
},
then: {
$mergeObjects: [
"$$rp",
{
locations: [
{
value: "India"
}
]
}
]
},
else: "$$rp"
}
}
}
}
}
}
])
The processing can also be done using $map operator. The following query can get us the expected output:
db.collection.aggregate([
{
$addFields:{
"relatedProperties":{
$map:{
"input":"$relatedProperties",
"as":"relatedProperty",
"in":{
"name":"$$relatedProperty.name",
"age":"$$relatedProperty.age",
"org":"$$relatedProperty.org",
"locations":{
$cond:[
{
$in:["$$relatedProperty.locations",[null,[]]]
},
[
{
"value":"India"
}
],
"$$relatedProperty.locations"
]
}
}
}
}
}
}
]).pretty()
Data set:
{
"_id" : ObjectId("5d666236986fb04b2aeabe2a"),
"relatedProperties" : [
{
"locations" : [
{
"value" : "Brazil"
},
{
"value" : "Germany"
}
],
"name" : "ABC",
"age" : "12",
"org" : {
"value" : "org1"
}
},
{
"locations" : [ ],
"name" : "CDE",
"age" : "30",
"org" : {
"value" : "org2"
}
},
{
"locations" : null,
"name" : "EFG",
"age" : "20",
"org" : {
"value" : "org3"
}
}
]
}
Output:
{
"_id" : ObjectId("5d666236986fb04b2aeabe2a"),
"relatedProperties" : [
{
"name" : "ABC",
"age" : "12",
"org" : {
"value" : "org1"
},
"locations" : [
{
"value" : "Brazil"
},
{
"value" : "Germany"
}
]
},
{
"name" : "CDE",
"age" : "30",
"org" : {
"value" : "org2"
},
"locations" : [
{
"value" : "India"
}
]
},
{
"name" : "EFG",
"age" : "20",
"org" : {
"value" : "org3"
},
"locations" : [
{
"value" : "India"
}
]
}
]
}

Inner array total count in mongodb

I have a journal doc. Which contains details of journal_volumes, journal issues and journal articles. I have to list the journals along with the count of volumes, issues and articles in each journal.
Here is my doc:
{
"_id" : ObjectId("5c470fc3135edb4413b0ea24"),
"jnl_code" : "KEG",
"jnl_volumes" : [
{
"name" : "1",
"created_date" : "2019-03-01",
"status" : "0",
"issue_flag" : "0",
"jnl_issues" : [
{
"issue_name" : "1",
"created_date" : "2019-03-04",
"jnl_articles" : [
"test",
"test2"
]
},
{
"issue_name" : "2",
"created_date" : "2019-03-04",
"jnl_articles" : [
"a"
]
},
{
"issue_name" : "3",
"created_date" : "2019-03-04",
"jnl_articles" : [
"b"
]
},
{
"issue_name" : "3",
"created_date" : "2019-03-05",
"jnl_articles" : [
"Q"
]
}
]
},
{
"name" : "2",
"created_date" : "2019-03-01",
"status" : "0",
"issue_flag" : "0",
"jnl_issues" : [
{
"issue_name" : "1",
"created_date" : "2019-03-05",
"jnl_articles" : [
"W"
]
},
{
"issue_name" : "1",
"created_date" : "2019-03-05",
"jnl_articles" : [
"S"
]
},
{
"issue_name" : "1",
"created_date" : "2019-03-05",
"jnl_articles" : [
"R"
]
},
{
"issue_name" : "1",
"created_date" : "2019-03-05",
"jnl_articles" : [
"R"
]
}
]
},
{
"name" : "3",
"created_date" : "2019-03-05",
"status" : "0",
"issue_flag" : "0"
}
]
}
My requirement is to get the count of jnl_volumes, total jnl_issues count and total jnl_articles count in single query..
Thanks to Neil Lunn to redirect me to the similar question (Calculate the count of nested objects with C#
). I referred the answer and wrote a query:
db.getCollection('rvh_journals').aggregate([
{
$project: {
"volumes" : { "$size" : { "$ifNull" : [ "$jnl_volumes", [] ] } },
"issues" : {
"$sum" : {
"$map" : {
"input" : "$jnl_volumes",
"in": { "$size" : { "$ifNull" : [ "$$this.jnl_issues", [] ] } }
}
}
},
"articles" : {
"$sum" : {
"$map" : {
"input" : "$jnl_volumes.jnl_issues.jnl_articles",
"in" : { "$size" : { "$ifNull" : [ "$$this", [] ] } }
}
}
}
}
}
])
This returns with an incorrect article count. Actual article count is 9 but the query returns 8
{
"_id" : ObjectId("5c470fc3135edb4413b0ea24"),
"volumes" : 3,
"issues" : 8,
"articles" : 8
}
Yes I got curious after your edit to the previous question, and noticed your statement was incorrect.
This one is correct:
db.getCollection('rvh_journals').aggregate([
{ "$project": {
"volumes": { "$size": "$jnl_volumes" },
"issues": {
"$sum": {
"$map": {
"input": "$jnl_volumes",
"in": { "$size": { "$ifNull": ["$$this.jnl_issues", [] ] } }
}
}
},
"articles": {
"$sum": {
"$map": {
"input": "$jnl_volumes",
"in": {
"$sum": {
"$map": {
"input": { "$ifNull": [ "$$this.jnl_issues", [] ] },
"in": { "$size": { "$ifNull": [ "$$this.jnl_articles", [] ] } }
}
}
}
}
}
}
}}
])
Returns:
{
"_id" : ObjectId("5c470fc3135edb4413b0ea24"),
"volumes" : 3,
"issues" : 8,
"articles" : 9
}
Note the traversal of the arrays.
You might go and read some of the actual words I used on that original linked answer, because I would have explained that nesting arrays like this is not a good idea. More details on why it's not a good idea and practical approaches to take otherwise are on Updating a Nested Array with MongoDB
enter image description here
.itcount() - is showing distinct count.
Need to show total count of count variable which is declare in $group
Currently it is showing based on company.

mongodb aggregation match multiple $and on the same field

i have a document like this :
{
"ExtraFields" : [
{
"value" : "print",
"fieldID" : ObjectId("5535627631efa0843554b0ea")
},
{
"value" : "14",
"fieldID" : ObjectId("5535627631efa0843554b0eb")
},
{
"value" : "POLYE",
"fieldID" : ObjectId("5535627631efa0843554b0ec")
},
{
"value" : "30",
"fieldID" : ObjectId("5535627631efa0843554b0ed")
},
{
"value" : "0",
"fieldID" : ObjectId("5535627631efa0843554b0ee")
},
{
"value" : "0",
"fieldID" : ObjectId("5535627731efa0843554b0ef")
},
{
"value" : "0",
"fieldID" : ObjectId("5535627831efa0843554b0f0")
},
{
"value" : "42",
"fieldID" : ObjectId("5535627831efa0843554b0f1")
},
{
"value" : "30",
"fieldID" : ObjectId("5535627831efa0843554b0f2")
},
{
"value" : "14",
"fieldID" : ObjectId("5535627831efa0843554b0f3")
},
{
"value" : "19",
"fieldID" : ObjectId("5535627831efa0843554b0f4")
}
],
"id" : ObjectId("55369e60733e4914550832d0"), "title" : "A product"
}
what i want is to match one or more sets from the ExtraFields array. For example, all the products that contain the values print and 30. Since a value may be found in more than one fieldID (like 0 or true) we need to create a set like
WHERE (fieldID : ObjectId("5535627631efa0843554b0ea"), value : "print")
Where i'm having problems is when querying more than one fields. The pipeline i came up with is :
db.products.aggregate([
{'$unwind': '$ExtraFields'},
{
'$match': {
'$and': [{
'$and': [{'ExtraFields.value': {'$in': ["A52A2A"]}}, {
'ExtraFields.fieldID': ObjectId("5535627631efa0843554b0ea")
}]
}
,
{
'$and': [{'ExtraFields.value': '14'}, {'ExtraFields.fieldID': ObjectId("5535627631efa0843554b0eb")}]
}
]
}
},
]);
This returns zero results, but this is what i want to do in theory. Match all items that contain set 1 AND all that contain set 2.
The end result should look like a faceted search output :
[
{
"_id" : {
"values" : "18",
"fieldID" : ObjectId("5535627831efa0843554b0f3")
},
"count" : 2
},
{
"_id" : {
"values" : "33",
"fieldID" : ObjectId("5535627831efa0843554b0f2")
},
"count" : 1
}
]
Any ideas?
You could try the following aggregation pipeline
db.products.aggregate([
{
"$match": {
"ExtraFields.value": { "$in": ["A52A2A", "14"] },
"ExtraFields.fieldID": {
"$in": [
ObjectId("5535627631efa0843554b0ea"),
ObjectId("5535627631efa0843554b0eb")
]
}
}
},
{
"$unwind": "$ExtraFields"
},
{
"$match": {
"ExtraFields.value": { "$in": ["A52A2A", "14"] },
"ExtraFields.fieldID": {
"$in": [
ObjectId("5535627631efa0843554b0ea"),
ObjectId("5535627631efa0843554b0eb")
]
}
}
},
{
"$group": {
"_id": {
"value": "$ExtraFields.value",
"fieldID": "$ExtraFields.fieldID"
},
"count": {
"$sum": 1
}
}
}
])
With the sample document provided, this gives the output:
/* 1 */
{
"result" : [
{
"_id" : {
"value" : "14",
"fieldID" : ObjectId("5535627631efa0843554b0eb")
},
"count" : 1
}
],
"ok" : 1
}