Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6374967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:33:15+00:00 2026-05-25T01:33:15+00:00

As part of my document in MongoDB I’m storing an array of objects. How

  • 0

As part of my document in MongoDB I’m storing an array of objects. How can I query it for only the 4th element of the array for example? So I don’t want the get the entire array out, just the 4th element.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:33:15+00:00Added an answer on May 25, 2026 at 1:33 am

    Use $slice.

    db.foo.find({ bar : "xyz" } , { my_array : { $slice : [n , 1] } } )
    

    will retrieve the nth element of the array “my_array” of all documents in the foo collection where bar = “xyz”.

    Some other examples from the MongoDB documentation:

    db.posts.find({}, {comments:{$slice: 5}}) // first 5 comments
    db.posts.find({}, {comments:{$slice: -5}}) // last 5 comments
    db.posts.find({}, {comments:{$slice: [20, 10]}}) // skip 20, limit 10
    db.posts.find({}, {comments:{$slice: [-20, 10]}}) // 20 from end, limit 10
    

    Which you can read here: http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to store java objects as part of the Solr document. They don't
I'm working with an OpenXML document, processing the main document part with some XSLT.
We have a web part that uploads a document to a document library. The
I'm working on a document application and part of this application I've to add
I'm working with an existing XML document which has a structure (in part) like
I am making a game. Part of the game is to collect objects. You
Using Symfony2 and Doctrine ODM with MongoDB, I need to access the parent document
this is the table from the official MEF document Part.Any Part.Shared Part.NonShared Import.Any Shared
I'm concerned about using $pop to remove elements of an array (embedded document) in
We have the sharepoint 2010 environment with Document ID's enabled. Given (part of) a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.