开发者

MongoDB C# Driver FindAndModify

开发者 https://www.devze.com 2023-04-10 12:31 出处:网络
I\'m trying to run a FindAndModify operation in MongoDB, but I\'m getting an unusual exception var query = Query.EQ(\"_id\", wiki.ID);

I'm trying to run a FindAndModify operation in MongoDB, but I'm getting an unusual exception

var query = Query.EQ("_id", wiki.ID);
var sortBy = SortBy.Descending("Version");
var update = Update.Set("Content", wiki.Content)
                    .Set("CreatedBy", wiki.CreatedBy)
                    .Set("CreatedDate", wiki.CreatedDate)
                    .Set("Name", wiki.Name)
                    .Set("PreviousVersion", wiki.PreviousVersion.ToBsonDocument())
                    .Set("Title", wiki.Title)
                    .Set("Version", wiki.Version);
var result = collection.FindAndModify(query, sortBy, update, true);

The exception I get is

WriteStartArray can only be called when State is Value, not when State is Initial
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the co开发者_如何学运维de.

Exception Details: System.InvalidOperationException: WriteStartArray can only be called when State is Value, not when State is Initial

Source Error:

Line 45:                 var query = Query.EQ("_id", wiki.ID);
Line 46:                 var sortBy = SortBy.Descending("Version");
Line 47:                 var update = Update.Set("Content", wiki.Content)
Line 48:                                    .Set("CreatedBy", wiki.CreatedBy)
Line 49:                                    .Set("CreatedDate", wiki.CreatedDate)

Thoughts? I've implemented this per the API on mongodb's site.

EDIT-- Fixed per @jeffsaracco

var update = Update.Set("Content", wiki.Content)
.Set("CreatedBy", wiki.CreatedBy)
.Set("CreatedDate", wiki.CreatedDate)
.Set("Name", wiki.Name)
.PushAllWrapped<WikiHistory>("PreviousVersion", wiki.PreviousVersion)
.Set("Title", wiki.Title)
.Set("Version", wiki.Version);


Your solution with PushAllWrapped may or may not be want you want. It is different from Set because it appends the new values to the current array value. If you wanted to replace the existing array value with a new array value you could use this version of Set:

var update = Update.Set("Content", wiki.Content)
    // other lines
    .Set("WikiHistory", new BsonArray(BsonDocumentWrapper.CreateMultiple(wiki.PreviousVersion);

which says: set the value of the WikiHistory element to a new BsonArray constructed by serializing the wrapped values of the custom type of PreviousVersion.


Are one of your columns an array type? If so, you might want to call

Update.Push

OR

Update.PushAll

for that column, and if PreviousVersion is already a BsonDocument you probably don't need to convert again


Are you sure of your SortBy.Descending ?

The api does not state that you could use somehting else than string[] as parameter (http://api.mongodb.org/csharp/current/html/96408b4e-c537-0772-5556-6f43805dd4d4.htm)


In the end, since I'm just tracking the history of the object, I ended up using .AddToSetWrapped<WikiHistory>("PreviousVersion", CurrentVersion) which just appends the item to the set in the object.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号