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

  • Home
  • SEARCH
  • 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 8684579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:20:35+00:00 2026-06-12T22:20:35+00:00

I have a document with embedded documents in an array. The prupose is to

  • 0

I have a document with embedded documents in an array. The prupose is to allow alias logins, that is: one user needs to be able to have several logins (username+company_id+pwd) since he or she may belong to different companies.

It looks somethine like this:

{
    name : "James D"
    logins : [
        {company_id : 1, username : "james", pwd : "****"},
        {company_id : 2, username : "james.d", pwd : "****"},
        {company_id : 3, username : "jd", pwd : "****"}
    ]
}

I want to index to company_id and username, which I did using

> db.user.ensureIndex({"logins.username" : "james", "logins.company_id" : 1})

And here’s my question, does the index work? Is the query acutally using the index? I’m asking because (1) I am not sure if MongoDB handles this kind of indexes, and (2) I’m not sure how to interpret the explain() function.

> db.user.find({"logins.username" : "james", "logins.company_id" : 1}).explain()
{
        "cursor" : "BtreeCursor username.color_1_logins.company_id_1",
        "nscanned" : 3,
        "nscannedObjects" : 3,
        "n" : 2,
        "millis" : 1,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "isMultiKey" : true,
        "indexOnly" : false,
        "indexBounds" : {
                "logins.username" : [
                        [
                                "blue",
                                "blue"
                        ]
                ],
                "logins.company_id" : [
                        [
                                {
                                        "$minElement" : 1
                                },
                                {
                                        "$maxElement" : 1
                                }
                        ]
                ]
        }
}
  • 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-12T22:20:36+00:00Added an answer on June 12, 2026 at 10:20 pm

    Those indexes are allowed and yours seems to be working. Every array element is added to the index seperately so indexing on large arrays might quickly increase the size of your index. For a few aliases of a username that should not be an issue though.

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

Sidebar

Related Questions

I have a document that has embedded documents in it but I am getting
Currently I have an address column that is an embedded document in my users
I have a document Design.rb that has an embedded document Attachment.rb that does some
I have a model pretty much like this: Document Release has an embedded array
So, I am having some issues with user authentication in embedded documents. I have
We have some embedded documents that look like this: { _id : ObjectId(4e402353bc9f6ec5a6000001), first_name
I have a Post document that has embedded tags. Sometimes I display only the
I have collection of documents, witch has an array, of embedded documents, how i
I am working with a complex mongodoc that has several embedded documents that represent
I have have a collection called users . Each user document has an embedded

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.