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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:14:59+00:00 2026-05-13T20:14:59+00:00

Here is the situation. I am inserting a new post and after insert I

  • 0

Here is the situation. I am inserting a new post and after insert I fetch the post and it works fine. Then I change one field and update which works fine. The problem occurs when I try to fetch the same post after the update. It always returns null.

     public class Post
        {
            public string _id { get; set; }
            public string Title { get; set; }
            public string Body { get; set; }
        }

 // insert a post 
        var post = new Post() {Title = "first post", Body = "my first post"};
        var posts = _db.GetCollection("posts");
        var document = post.ToDocument();

        // inserts successfully! 
        posts.Insert(document);

        // now get the post 
        var spec = new Document() {{"_id", document["_id"]}};

        // post was found success
        var persistedPost = posts.FindOne(spec).ToClass<Post>();

        persistedPost.Body = "this post has been edited again!!";
        var document2 = persistedPost.ToDocument(); 
        // updates the record success although I don't want to pass the second parameter
        posts.Update(document2,spec);

        // displays that the post has been updated
        foreach(var d in posts.FindAll().Documents)
        {
            Console.WriteLine(d["_id"]); 
            Console.WriteLine(d["Body"]);
        }

    // FAIL TO GET THE UPDATED POST. THIS ALWAYS RETURNS NULL ON FindOne call! 
    var updatedPost = posts.FindOne(new Document() {{"_id",document["_id"]}}).ToClass<Post>(); // this pulls back the old record with Body = my first post
    Assert.AreEqual(updatedPost.Body,persistedPost.Body);

UPDATE:

I think I have resolved the problem but the issue is very weird. See the last line.

var updatedPost = posts.FindOne(new Document() {{"_id",document["_id"]}}).ToClass<Post>();

The FindOne method takes new document which depends on document[“_id”]. Unfortunately, that does not work and for some reason it requires you to send the _id associated with persistedPost update which you will get after the update command. Here is the example:

 var persistedPost = posts.FindOne(spec).ToClass<Post>();
            persistedPost.Body = "this is edited";
            var document2 = persistedPost.ToDocument(); 
            posts.Update(document2,new Document() {{"_id",document["_id"]}});

            var updatedPost = posts.FindOne(new Document(){{"_id",document2["_id"]}}).ToClass<Post>(); 
            Console.WriteLine(updatedPost.Body);

See, now I am sending the document2[“_id”] instead of the document field. This seems to work correctly. I guess the 24 byte code that it generates for each “_id” field is different.

  • 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-13T20:15:00+00:00Added an answer on May 13, 2026 at 8:15 pm

    The answer is that not to rely on the “_id” generated by MongoDb. Just use your own unique identifier like Guid or identity.

    UPDATE:

    My ToDocument method was putting the _id as string you must always put _id as Oid.

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

Sidebar

Ask A Question

Stats

  • Questions 356k
  • Answers 356k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Is this a known issue with Scripting Bridge, or am… May 14, 2026 at 8:59 am
  • Editorial Team
    Editorial Team added an answer I know that in Visual Studio, executing code as part… May 14, 2026 at 8:59 am
  • Editorial Team
    Editorial Team added an answer As Pointy pointed out (excuse the pun), jQuery messes with… May 14, 2026 at 8:59 am

Related Questions

Does anyone know how to have dependency injection work with linq2sql. Heres my situation..
I am working in cross platform C++, and have some classes defined like so:
I have a situation where I think using the 'em' metric would be perfect.
Here is the situation. I am making changes to an application but I do
Here is the situation : If I am in page-1 now I am clicking

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.