InsertOneAsync(..) How do I get the ObjectId after insertion

3,435 views
Skip to first unread message

Jürgen Stolz

unread,
Sep 22, 2015, 12:12:30 PM9/22/15
to mongodb-csharp
Hi,
  • MongoDb >3.0
  • Driver > 2.01 (async)
After an insertion I want to return the object  extended with the automatically generated ObjectId. How do I get this value?
Some source code
        public static Task Insert_Temp(DBContentBase insertObject)
        {
            if (insertObject.DBVersion != 0)
            {
                throw new Exception("Insert not allowed");
            }
            insertObject.Created = DateTimeOffset.Now;
            insertObject.Updated = insertObject.Created;
            insertObject.DBVersion = 1; 
            var collection = mongoDatabase.GetCollection<BsonDocument>(insertObject.GetType().Name);
            Task insertOneAsync = collection.InsertOneAsync(insertObject.ToBsonDocument());
            insertOneAsync.Wait();

            return insertOneAsync;
        }

The Task is finished but I didn´t find the Id.

Regards Jürgen

Craig Wilson

unread,
Sep 22, 2015, 12:57:23 PM9/22/15
to mongodb-csharp
The inserted object will have been mutated to include the identifier. Is that not the case?

Jürgen Stolz

unread,
Sep 22, 2015, 1:18:20 PM9/22/15
to mongodb-csharp
No the ObjectId in from the object (DBContentBase insertObject) I insert is still empty and in the Task object after the wait() I did not find the Id. In the Database itself the object is inserted with a generated id.
I insert a Person
Source snippets of the classes:
    public class DBContentBase
    {
        public ObjectId Id { get; set; }
        public int DBVersion { get; set; }
        public DateTimeOffset Created { get; set; }
        public DateTimeOffset Updated { get; set; }
    } 

   public class Person : DBContentBase
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Info { get; set; }
        public string Concatenated { get; set; }
        public DateTimeOffset DateOfBirth { get; set; }
        public DateTimeOffset Marriage { get; set; }

        public int Age { get; set; }
        public string Profession { get; set; }

Craig Wilson

unread,
Sep 22, 2015, 3:35:25 PM9/22/15
to mongodb-csharp
If that is true, then it's a bug. Please file an issue at http://jira.mongodb.org under the CSHARP project with a reproducible sample and we'll get it fixed.

Jürgen Stolz

unread,
Sep 23, 2015, 4:31:42 AM9/23/15
to mongodb-csharp
I created the jira.
CSHARP-1421 Missing the generated ObjectId after inserting an object

Regards
Jürgen

Jürgen Stolz

unread,
Sep 23, 2015, 7:18:53 AM9/23/15
to mongodb-csharp
I found the reason
Sorry please close the jira. After changing the Class.Type from BsonDocument to the real Class.Type DBContentBase (in the Insert(...) method it worked. 

Craig Wilson

unread,
Sep 23, 2015, 8:03:12 AM9/23/15
to mongodb-csharp
Thanks Jürgen,

I'm glad you found the problem.

Craig 
Reply all
Reply to author
Forward
0 new messages