C# Driver - Update.Push() overwrites array.

1,300 views
Skip to first unread message

Wade K

unread,
Dec 14, 2011, 7:12:01 PM12/14/11
to mongodb-user
I am trying to add an audit trail to my documents using the
Update.Push(). The general idea, is that every time the document is
saved, i want to add an element to an array that will mark the status
that the document was updated to and the timestamp that the update was
made. However, when I run this, instead of appending the new entry to
the end of the History array, the History array only holds a single
element which is the latest (previous entries are overwritten.). So
the query part is working, but the update portion is acting more like
a $set. Am I missing an option parameter here? Manually running this
in the shell seems to work fine.

Thanks.

Sample Code:

public bool UpsertFileStatus(FileStatus status)
{
MongoDatabase db =
MongoDatabase.Create(ConfigurationManager.ConnectionStrings[FileStatusDBName].ConnectionString);

SafeModeResult res =
db.GetCollection(FileStatusCollectionName).Save<FileStatus>(status,
SafeMode.True);

// Add to audit trail
BsonElement[] elements = new BsonElement[2];
elements[0] = new BsonElement("Status",
status.Status.ToString());
elements[1] = new BsonElement("UpdateUTC", DateTime.UtcNow);

BsonDocument historyEntry = new BsonDocument(elements);


db.GetCollection(FileStatusCollectionName).Update(Query.EQ("_id",
status.Filename), Update.Push("History", historyEntry));

return res.DocumentsAffected > 0;
}

Nat

unread,
Dec 14, 2011, 8:24:32 PM12/14/11
to mongod...@googlegroups.com
Have you tried to turn profiling on to see what's getting executed? Everything looks ok except that your history can be overwritten by the "Save" function you had earlier in your code?
--
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.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Wade K

unread,
Dec 15, 2011, 12:10:12 PM12/15/11
to mongodb-user
Thanks, Nat. The Save was the problem. I modified my Save logic to
do a series of "Update.Set" operations and used the Update call. This
left the array intact and available for insert.

On Dec 14, 6:24 pm, "Nat" <nat.lu...@gmail.com> wrote:
> Have you tried to turn profiling on to see what's getting executed? Everything looks ok except that your history can be overwritten by the "Save" function you had earlier in your code?
>
>
>
>
>
>
>
> -----Original Message-----
> From: Wade K <wka...@gmail.com>
> Sender: mongod...@googlegroups.com
> Date: Wed, 14 Dec 2011 16:12:01
> To: mongodb-user<mongod...@googlegroups.com>
> Reply-To: mongod...@googlegroups.com
> Subject: [mongodb-user] C# Driver - Update.Push() overwrites array.
>
> I am trying to add an audit trail to my documents using the
> Update.Push().  The general idea, is that every time the document is
> saved, i want to add an element to an array that will mark the status
> that the document was updated to and the timestamp that the update was
> made.  However, when I run this, instead of appending the new entry to
> the end of the History array, the History array only holds a single
> element which is the latest (previous entries are overwritten.).  So
> the query part is working, but the update portion is acting more like
> a $set.  Am I missing an option parameter here?  Manually running this
> in the shell seems to work fine.
>
> Thanks.
>
> Sample Code:
>
> public bool UpsertFileStatus(FileStatus status)
>     {
>       MongoDatabase db =

> MongoDatabase.Create(ConfigurationManager.ConnectionStrings[FileStatusDBNam e].ConnectionString);

Reply all
Reply to author
Forward
0 new messages