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 7784599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:07:14+00:00 2026-06-01T20:07:14+00:00

Mongo DB: I’m looking to make one query to return both the first and

  • 0

Mongo DB: I’m looking to make one query to return both the first and last element of an array. I realize that I can do this multiple queries, but I would really like to do it with one.

Assume a collection “test” where each objects has an array “arr” of numbers:

 db.test.find({},{arr:{$slice: -1},arr:{$slice: 1}});

This will result in the following:

 { "_id" : ObjectId("xxx"), "arr" : [ 1 ] }    <-- 1 is the first element

Is there a way to maybe alias the results? Similar to what the mysql AS keyword would allow in a query?

  • 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-06-01T20:07:15+00:00Added an answer on June 1, 2026 at 8:07 pm

    This is not possible at the moment but will be with the Aggregation Framework that’s in development now if I understand your functional requirement correctly.

    You have to wonder about your schema if you have this requirement in the first place though. Are you sure there isn’t a more elegant way to get this to work by changing your schema accordingly?

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

Sidebar

Related Questions

Mongo docs state: The Mongo multikey feature can automatically index arrays of values. That's
In Mongo my understanding is that you can have databases and collections. I'm working
I can't seem to connect to Mongo DB, which I've installed as a Windows
I have a mongo query to be executed : query = { dateField :
I have documents in Mongo that contain questionnaire-like data (company's name, address, phone numbers,
I am using mongo-ruby-driver, I want to get stats/query graphs to show for Mongo.
If I have a mongo instance running, how can I check what port numbers
Possible Duplicate: Mongo interface I am new to mongo db, and i am looking
In ruby using the mongo gem I can't find any documentation on how to
Where can I get mongo php driver for PHP 5.4? For windows vc9 not

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.