Reputation: 443
I am trying to find out if a document exists in a collection using the code below. Whenever the query doesn't find any documents I get a StackOverflowException. What am I doing wrong with this?
MongoServer server = MongoServer.Create(connectionString);
MongoDatabase db = server.GetDatabase(database);
MongoCollection<Document> documents = db.GetCollection<Document>("Documents");
var query = Query.EQ("DocID", doc.DocID);
var result = documents.FindOneAs<Document>(query);
if (result != null)
{
doc.Id = result.Id;
doc.DocCreated = result.DocCreated;
doc.DocCreatedBy = result.DocCreatedBy;
doc.MergeFiles(result);
documents.Save(doc);
}
else
{
doc.Save();
}
Also I am using the official mongodb c# driver.
Edit: Here is the stack trace. It doesn't really say much.
An unhandled exception of type 'System.StackOverflowException' occurred in System.dll Cannot evaluate expression because the current thread is in a stack overflow state.
Edit 2: Here is a link to my document class. https://gist.github.com/68d38bec41ebc46f30eb
Upvotes: 1
Views: 1553
Reputation: 3503
Your else condition doesn't seem to involve any mongo related code. doc.Save() calls DocumentData.Save passing this (Document) as an argument. I can only guess that somewhere in that call chain it eventually calls back do Document.Save again.
Upvotes: 1