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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:57:08+00:00 2026-05-31T07:57:08+00:00

I’m implementing a contact database that handles quite a few fields. Most of them

  • 0

I’m implementing a contact database that handles quite a few fields. Most of them are predefined and can be considered bound, but there are a couple that aren’t. We’ll call one of these fields ‘groups’. The way we currently implement it is (each document/contact has ‘groups’ field):

'groups' : {
   152 : 'hi',
   111 : 'group2'
}

but after some reading I’ve it would seem I should be doing it:

'groups' : [
   { 'id' : 152, 'name' : 'hi' },
   { 'id' : 111, 'name' : 'group2' }
   ...
]

and then apply the index db.contact.ensureIndex({'groups.id':1});

My question is in regard to functionality. What are the differences between the 2 structures and how is the index actually built (is it simply indexing within each document/contact or is it building a full-scale index that has all the groups from all the documents/contacts?).

I’m kind of going in under the assumption that this is structurally the best way, but if I’m incorrect, let me know.

  • 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-31T07:57:09+00:00Added an answer on May 31, 2026 at 7:57 am

    Querying will certainly be a lot easier in the second case, where ‘groups’ is an array of sub-documents, each with an ‘id’ and a ‘name’.

    Mongo does not support “wildcard” queries, so if your documents were structured the first way and you wanted to find a sub-document with the value “hi”, but did not know that the key was 152, you would not be able to do it. With the second document structure, you can easily query for {“groups.name”:”hi”}.

    For more information on querying embedded objects, please see the documentation titled “Dot Notation (Reaching into Objects)” http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29
    The “Value in an Array” and “Value in an Embedded Object” sections of the “Advanced Queries” documentation are also useful:
    http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-ValueinanArray

    For an index on {‘groups.id’:1}, an index entry will be created for every “id” key in every “groups” array in every document. With an index on “groups”, only one index entry will be created per document.

    If you have documents of the second type, and an index on groups, your queries will have to match entire sub-documents in order to make use of the index. For example, given the document:

    { "_id" : 1, "groups" : [ { "id" : 152, "name" : "hi" }, { "id" : 111, "name" : "group2" } ] }
    

    The query

    db.<collectionName>.find({groups:{ "id" : 152, "name" : "hi" }}) 
    

    will make use of the index, but the queries

    db.<collectionName>.find({"groups":{$elemMatch:{name:"hi"}}})
    

    or

    db.<collectionName>.find({"groups.name":"hi"})
    

    will not.

    The index(es) that you create should depend on which queries you will most commonly be performing.

    You can experiment with which (if any) indexes your queries are using with the .explain() command. http://www.mongodb.org/display/DOCS/Explain The first line, “cursor” will tell you which index is being used. “cursor” : “BasicCursor” indicates that a full collection scan is being performed.

    There is more information on indexing in the documentation:
    http://www.mongodb.org/display/DOCS/Indexes

    The “Indexing Array Elements” section of the above links to the document titled “Multikeys”:
    http://www.mongodb.org/display/DOCS/Multikeys

    Hopefully this will improve your understanding of how to query on embedded documents, and how indexes are used. Please let us know if you have any follow-up questions!

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do 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.