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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:56:02+00:00 2026-05-22T18:56:02+00:00

If I want to read and write mongo data with a POCO public class

  • 0

If I want to read and write mongo data with a POCO

public class Thingy
{
     public string Foo {get;set;}
}
...
coll.Insert(new Thing(Foo = "hello"));

When I read back I get a failure saying that _id is an unexpected attribute (which it is). So then I added a field called _id to the class. Now the insert doesnt work saying that the _id field cannot be null. A tried BsonIgnoreIfNull attribute, that didnt work.

  • 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-22T18:56:03+00:00Added an answer on May 22, 2026 at 6:56 pm

    When you insert an object, if it doesn’t have an _id field then the driver adds one and sets it to a 12-byte MongoDB ObjectId value.

    You just need to add an Id property to your POCO, which will be deserialised from _id:

    public class Thingy
    {
         public ObjectId Id { get; set; }
    }
    

    Or, if you’d like to delegate another property to map onto _id then you can decorate it with the BsonIdAttribute, like this:

    [BsonId]
    public ObjectId MyKey { get; set; }   
    

    The _id field doesn’t have to be an MongoDB ObjectId, you can set it to any value of any data type (except an array), it just needs to be unique within the collection.

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

Sidebar

Related Questions

i want simultaneously read and write data into file. Can i use StreamReader and
I want to interrupt some specific grails domain class events(read,write,delete,update).Is there any hibernate eventlistner
I just want to read (and maybe write) UTF-8 data. haskell.org still advertises System.Streams
I have a asp.net webservice running in windows server 2003, i want it read/write
I want to read and write from serial using events/interrupts. Currently, I have it
How can I read/write serializable object instances to a RandomAccessFile in Java? I want
I want to read, write and create Spreadsheets in the Open Document Format with
I want to read multiple RSS feeds using jQuery. I'm trying to write a
I want to read an xml file, apply a transform, then write to another
I want to write a program in C/C++ that will dynamically read a web

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.