Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting array elements using array index in mongoDB

Consider following collection in mongoDB :

{a:[4,2,8,71,21]}
{a:[24,2,2,1]}
{a:[4,1]}
{a:[4,2,8,21]}
{a:[2,8,71,21]}
{a:[4,2,8]}

How can I get following results in a most easily:

Getting nth element of array

{a:4}
{a:24}
{a:4}
{a:4}
{a:2}
{a:4}

Getting elements 2 to 4

{a:[8,71,21]}
{a:[2,1]}
{a:[]}
{a:[8,21]}
{a:[71,21]}
{a:[8]}

And other similar queries.

like image 288
Handsome Nerd Avatar asked Nov 28 '25 22:11

Handsome Nerd


1 Answers

What you are looking for is the $slice projection.

Getting a number of elements from the beginning of an array

You can pass a simple $limit with a number of values to return (eg. 1):

> db.mycoll.find({}, {_id: 0, a: { $slice: 1}})
{ "a" : [ 4 ] }
{ "a" : [ 24 ] }
{ "a" : [ 4 ] }
{ "a" : [ 4 ] }
{ "a" : [ 2 ] }
{ "a" : [ 4 ] }

Getting a range of elements

You can pass an array with parameters of ( $skip, $limit ).

Note: to match your expected output you would have to find elements 3 to 5 (skip the first 2 elements, return the next 3):

> db.mycoll.find({}, {_id: 0, a: { $slice: [2,3]}})

{ "a" : [ 8, 71, 21 ] }
{ "a" : [ 2, 1 ] }
{ "a" : [ ] }
{ "a" : [ 8, 21 ] }
{ "a" : [ 71, 21 ] }
{ "a" : [ 8 ] }

Getting the nth element of array

Pass the number of elements to $skip and a value of 1 for the limit.

For example, to find the second element you need to skip 1 entry:

> db.mycoll.find({}, {_id: 0, a: { $slice: [1,1]}})

{ "a" : [ 2 ] }
{ "a" : [ 2 ] }
{ "a" : [ 1 ] }
{ "a" : [ 2 ] }
{ "a" : [ 8 ] }
{ "a" : [ 2 ] }

Note that the $slice operator:

  • always returns an array
  • will return an empty array for documents that match the find criteria but return an empty result for the $slice selection (eg. if you ask for the 5th element of an array with only 2 elements)
like image 197
Stennie Avatar answered Dec 01 '25 11:12

Stennie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!