How to convert primitive array into object array with key and value?

Hi, i have an array like this
array: [0,1,2,4,5,7,86,4,32,3,4]
i need to assign a key to them all like the result will be:

array: [{“key”: 0}, {“Key”:1},… so on to all.
by Aggregation only.
thanks

2 Likes

@Brutal_Jazzy, you can try this aggregation query. To convert the primitive element in the array field to an object, use the $map operator to iterate over the array field and transform each element (as needed).

db.collection.aggregate([
{ 
  $addFields: { 
      array: { 
         $map: { 
             input: "$array", 
             as: "e", 
             in: { key: "$$e" } 
         }
      }
  } 
},
])
2 Likes

Thanks man it helped a lot. once last help needed.
let say the array has repeating values
[1, 1, 2, 3, 4, 5, 5, 6, 7]

now i want to give two keys to each
[{item: 1
timesRepeated: 2}, so on]
how we do it?
Thanks a lot sir.

1 Like

Here is the query for that.

db.collection.aggregate([ 
{ 
  $unwind: "$array" 
}, 
{ 
  $group: { 
      _id: { _id: "$_id", a: "$a" }, 
      count: { $sum: 1 } 
  } 
},
{ 
  $sort: { _id: 1 }
},
{ 
  $group: { 
      _id: "$_id._id", 
      array: { $push: { item: "$_id.array", repeated: "$count" }} 
  }
} 
])
2 Likes

Thanks again Sir,
Without using group and unwind?
something like this?

{
atm: {
$map: {
input: ‘$am’,
as: ‘o’,
‘in’: {
a: ‘$$o’,
c: {
$cond: {
‘if’: {
or reduce here to count etc?
then: ‘N’,
‘else’: ‘P’
}
},
t: 0
}
}
}
}

1 Like

@Brutal_Jazzy, I am afraid you have to try that yourself :slight_smile: . Also, please do post further questions you have as new posts.

1 Like

Kindly, i have tried at least 20 times in the last 24 hours i am stuck at this. i cannot use:
Sort and Unwind actually.

Please elaborate on the reasons why

Hi, because i have used sort too many times and it might over use Ram and give errors. as for the unwind my document is large and i have used unwind few times already and i will have to use group again.
I am thinking something like reduce.

Sir i have added a new question which explains very well i am sorry for the disturbance please check this question out:

1 Like

please check this:

1 Like

You don’t need to use the sort stage in the query - the query will work the same. It is there so that the elements in the array are ordered.

Have you tried the query? Do you have any errors when you run the query?

Even if you use $reduce, it still requires the same kind of memory - it has to do the group and count (or even the sort) to calculate the output you are expecting. It is a stateful operation too (like the sort and the group are). Calculating sums, counts, averages, minimum, maximum, etc., are all some kind of reduction operations and are stateful (or blocking). They will need the RAM.

2 Likes

your query run as charm there were no errors. but now i am trying to remove one step which adds the keys to the array elements please check my other question i will be thankful.
thanks

1 Like

the only problem this aggregate has it doesn’t copy the old values if you can add that to this query it is also fine with me. it gives me the single document which has the array and id but i have other fields in the previous document from which we use $unwind thanks

1 Like
db.collection.aggregate([ 
{ 
  $unwind: "$a" 
}, 
{ 
  $group: {
      _id: { _id: "$_id", a: "$a" },
      doc: { $first: "$$ROOT" },
      count: { $sum: 1 } 
  } 
},
{ 
  $group: { 
      _id: "$_id._id", 
      doc: { $first: "$doc" },
      a: { $push: { item: "$_id.a", repeated: "$count" }}, 
  }
},
{
  $addFields: {
      "doc.a": "$a"
  }
},
{
  $replaceWith: "$doc"
}
])

NOTE:

  1. I have used the field name as a, instead of array.
  2. I have removed the $sort stage (you can still use it).
3 Likes

Great man thank you very very very very very very much. May you have many many more knowledge and keep helping others.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.