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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:53:34+00:00 2026-05-28T07:53:34+00:00

On this post MongoDB finding nested elements , author claims that the mongodb document

  • 0

On this post MongoDB finding nested elements, author claims that the mongodb document structure is

      car : { "$ref" : "cars" , "$id" : { "$oid" : "4e8478ace4b0dea06288ad63"}}

When i tried to reproduce the problem, i encountered some weird behaviors with mongodb insertion

When run the insertion on above sample data, i got following error

> db.sample.insert({car:{ "$ref" : "cars" , "$id" : { "$oid" : "4e8478ace4b0dea06288ad63"}}})
Tue Jan 24 14:09:07 uncaught exception: field names cannot start with $ [$oid]

it says that field names cannot start with $.

If thats the case, it should not work if i remove $ from oid and left the remaining $ref & $id untouched

    > db.sample.insert({car:{ "$ref" : "cars" , "$id" : { "oid" : "4e8478ace4b0dea06288ad63"}}})
    > db.sample.find()
{ "_id" : ObjectId("4f1e6fbc403aae757ec6dea5"), "car" : { "$ref" : "cars", "$id" : { "oid" : "4e8478ace4b0dea06288ad63" } } }

surprisingly it worked. Now it accepts the fields start with $

Also when i tried this query

> db.sample.insert({ "$ref" : "cars" })
document to insert can't have $ fields

i got the error back.

I don’t understand what causes this? anybody have a clear idea?

  • 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-28T07:53:34+00:00Added an answer on May 28, 2026 at 7:53 am

    $id and $ref are special identifiers used for dbrefs. Otherwise, field names starting with a $ aren’t allowed.

    However, your first-level document must not be a dbref itself, hence the error “document to insert can’t have $ fields”.

    However, dbrefs are allowed as subdocuments, e.g. (from the official docs)

    "name" : "Joe",
    "classes" : [
            {
                    "$ref" : "courses",
                    "$id" : ObjectId("4b0552b0f0da7d1eb6f126a1")
            }
    ]
    

    Now $oid is not a special identifier, and is not allowed because the $ has special semantics: Think of $inc. That is an operator, but if $ field names were allowed, it could also be the name of a field.

    You have to be careful when using the positional operator in updates:

    The positional operator cannot be combined with an upsert since it requires a matching array element. If your update results in an insert then the “$” will literally be used as the field name.

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

Sidebar

Related Questions

This post says that you need a 64 bit system for MongoDB if your
I have a document structure in MongoDB like this: User |---> Posts |----> Comments
This post says that it is possible to turn off the CLR flag for
By following the official instructions http://www.mongodb.org/display/DOCS/Quickstart+Unix and this post http://blog.phy5ics.com/2010/03/27/installing-mongodb-on-mediatemple-dv/ I've just about managed
First to say that I'm new to MongoDb and document oriented db's in general.
This post relates to a previous that i posted but the question is different
This post reference to the One Definition Rule. Wikipedia is pretty bad on explaining
This post on SO answers most of the questions I have (much thanks to
This post asks this question but doesn't really give an answer, so I thought
This post relates to this: Add row to inlines dynamically in django admin Is

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.