Re: [mongodb-user] Batch Save to MongoDB C# Driver

917 views
Skip to first unread message

Robert Stam

unread,
May 21, 2013, 10:22:07 AM5/21/13
to mongod...@googlegroups.com
You have to save the documents one at a time:

    foreach (var document in autolookuplist)
    {
        idcollection.Save(document);
    }

Why don't you just save them one at a time in the loop (using your commented out call to Save)?


On Tue, May 21, 2013 at 9:00 AM, mark gamache <gamach...@gmail.com> wrote:
I've asked this on StackOverflow but I think this might be a better place:

I am having difficulty batch saving MongoDB documents using the C# driver. Here's sample code that inserts a Document with a GUID BsonID. Saving one at a time works. If I insert the documents into a list and use the save command it fails with an "Save can only be used with documents that have an Id."error. I think it might be a bug in the driver not recognizing the custom ID field, bu I am not sure. Any thoughts?

var autolookuplist = new List<BsonDocument>();
            Parallel.ForEach(docs, webdoc =>
                {
                    lock (autolookuplist)
                    {
                        autoID++;
                        var hold = new APAUtoIDGuidLookup() {AutoIncrementID = autoID, ID = webdoc.ID};
                      autolookuplist.Add(hold.ToBsonDocument());
                    }

                  //this works
                 //   idcollection.Save(new APAUtoIDGuidLookup() { AutoIncrementID = autoID, ID = webdoc.ID });
                });


          //this doesn't work
          idcollection.Save(autolookuplist);

here's the document class

public class  APAUtoIDGuidLookup
{
    [BsonId]
    public Guid ID { get; set; }
    public int AutoIncrementID { get; set; }
}





--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

mark gamache

unread,
May 21, 2013, 10:32:07 AM5/21/13
to mongod...@googlegroups.com
Thanks, I may have to save one at a time, but it's about performance.This server will be under considerable load and if I have 1000 (or more) documents to update I would prefer that it be 1 network/DIsk IO access instead of 1000.

This is a bug/oversight in the C# driver. It looks like MongoDB supports batch updates, it's just the C# driver doesn't.   

Bernie Hackett

unread,
May 21, 2013, 10:41:04 AM5/21/13
to mongod...@googlegroups.com
This is a bug/oversight in the C# driver. It looks like MongoDB supports batch updates, it's just the C# driver doesn't.

Robert Stam

unread,
May 21, 2013, 10:41:18 AM5/21/13
to mongod...@googlegroups.com
The current version of the server only supports batch inserts, it does not support batch updates. There are plans to support batch updates in the future.

Save is based on top of Update because it works with either new documents or existing documents.

If you know that all your documents are new (like they are in your case), you could use InsertBatch instead.

mark gamache

unread,
May 21, 2013, 11:11:36 AM5/21/13
to mongod...@googlegroups.com
I guess it doesn't thanks for the heads up. That's disappointing news and it looks like that jira ticket is old, so it's probably not going to happen soon. Once the system is in production 90% of the calls will be updates and not inserts so I'll have to stick with one-at-a-time. 
Reply all
Reply to author
Forward
0 new messages