简体   繁体   中英

How can I access array element by index using MongoDB 3.0?

I would like to know how I can access the elements of an array in mongo (version 3.0) by position, just as I would with $arrayElemAt in the newer versions.

UPDATED: I want to achieve the same of this example using mongodb 3.0:

在此处输入图片说明

Thank you very much.

In this case $arrayElementAt returns only one element from a given position. If you want to manipulate a chunk of items you want to return you should use $slice. Please check the link for more references: MongoDB $slice

{ $slice: 3 } //for the first 3
{ $slice: -3 } //for the last 3
{ $slice: 2, 3 } //for 3 elements after skiping the 2 frist

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM