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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:21:04+00:00 2026-06-02T02:21:04+00:00

Not sure if this is possible, but here goes. I have a MongoDB (using

  • 0

Not sure if this is possible, but here goes.

I have a MongoDB (using morphia to access it) with a table like the following (simplified for this example, but the concept is the same):

{ Name : "Robert", NickNames : [ "Bob", "Twinkletoes" ] }
{ Name : "Trevor", NickNames : [ "Bob", "Mad man", "Tweedledee" ] }
{ Name : "Samuel", NickNames : [ "Sam" ] }
{ Name : "Patrick", NickNames : [ "Bob", "Mad man" ] }
{ Name : "Mark", NickNames : [ "Adam", "Bob", "Georg" ] }

And for the purpose of this example let’s say the nicknames are always in alphabetical order and the position where the nickname lies is relevent.

So, as we can see every person record can have a different number of nicknames in the list. How can I perform the following searches:

  • Give me all people with the first nickname “Bob” and the second nickname “Mad man” (i.e. Person.Nicknames[0] = “Bob” && Person.Nicknames[1] = “Mad man”)

    Should return 2 records – Trevor and Patrick.

  • Give me all people with the first nickname “Bob” (i.e. Person.Nicknames[0] = “Bob”)

    Should return 3 records – Robert, Trevor and Patrick.

Note that Mark wasn’t returned because he his nickname of “Bob” wasn’t in the first index.

Is there any way to do this?

  • 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-02T02:21:06+00:00Added an answer on June 2, 2026 at 2:21 am

    Once elements are inserted into an array, their order will be preserved.

    The queries should work as you expect them to: (_id omitted for brevity)

    > db.people.find({"NickNames.0":"Bob", "NickNames.1":"Mad man"}, {_id:0})
    { "Name" : "Trevor", "NickNames" : [ "Bob", "Mad man", "Tweedledee" ] }
    { "Name" : "Patrick", "NickNames" : [ "Bob", "Mad man" ] }
    > db.people.find({"NickNames.0":"Bob"}, {_id:0})
    { "Name" : "Robert", "NickNames" : [ "Bob", "Twinkletoes" ] }
    { "Name" : "Trevor", "NickNames" : [ "Bob", "Mad man", "Tweedledee" ] }
    { "Name" : "Patrick", "NickNames" : [ "Bob", "Mad man" ] }
    > 
    

    There is a brief note on this in the “Array Element by Position” section of the “Dot Notation (Reaching into Objects)” documentation:
    http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29#DotNotation%28ReachingintoObjects%29-ArrayElementbyPosition

    The above was done in the JS shell. I am not familiar with Morphia, so you will have to translate the queries into the correct Morphia syntax yourself. Hope this helps!

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

Sidebar

Related Questions

I'm not sure if this is even possible but here goes: Currently I have
I'm not sure this is even possible, but here goes: I have a class
I'm not sure if this is even possible, but here goes: I have a
Not sure if this is possible but here is what I would like to
I'm not sure if this is even possible but here we go. I have
Not sure how to ask this question, but here goes. Say I have: var
I'm not sure if this is even remotely possible but here it goes: is
I'm not sure if this is possible but I'd like to be able to
I'm not sure if this is possible (complete non-flash developer speaking), but we have
I'm not sure exactly how to describe this question, but here goes. I've got

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.