[CSHARP-530] Dublicate Key error while upset Created: 17/Jul/12  Updated: 05/Apr/19  Resolved: 22/Aug/12

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: erhan Assignee: Robert Stam
Resolution: Cannot Reproduce Votes: 0
Labels: crash, driver, dublicate, error, key, question, upsert
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

MongoDB runs on ubuntu server lts 12, development environment is Windows 2008 R2 Ent Server with VS 2010



 Description   

When I want to upsert my record I get duplicate key error.
The Code:
var update_query = Query.And(
Query.EQ("Id", item.Id) , ,
Query.EQ("UserId", item.UserId),
Query.EQ("Email",item.Email)
);
var resxs= coll.Update(update_query,
Update.Replace(item),
UpdateFlags.Upsert
);

But when I remove "Id" criteria,the code works and doing upset.
Is there anything I am missing.

thnx



 Comments   
Comment by Robert Stam [ 22/Aug/12 ]

Closing this ticket as unable to reproduce (and no further information provided).

Comment by Robert Stam [ 19/Jul/12 ]

One thing that seems odd is that the JSON document returned by item.ToJson() does not have an "_id" element (but it does have an "Id" element). Normally a C# property called "Id" is automatically mapped to "_id" in the database. Can you show your class definition for item?

There is also something odd about BirthDate. In the database they are stored as BSON DateTime values (see ISODate), but in the document returned by item.ToJson() they appear to be strings formatted in an unusual way ("/Date(612824400000+0300)/").

Comment by erhan [ 19/Jul/12 ]

Before modify the record :
> db.user.find({_id:1})

{ "_id" : NumberLong(1), "UserId" : "20277B19-F603-4CA1-8B3A-B2A009BB291A-@o@-1E43F402-0A2E-4E11-9708-15F7AD9EFA82", "Email" : "xxxxxx@hotmail.com", "FirstName" : "xxxx", "LastName" : "yyyyy", "BirthDate" : ISODate("1989-06-02T21:00:00Z"), "Password" : "test", "Gender" : 0, "CityId" : 7, "TownId" : 0, "IsDisabled" : 1, "IsGlobalDisabled" : 0, "IsRegKeySent" : -1, "IsRegKeyValid" : -1, "LastSeenOn" : null, "ParentUserId" : null, "RegisteredOn" : null, "ValidatedOn" : null, "ParishId" : 0, "CityName" : "-", "TownName" : null, "ParishName" : null, "IsSentToSql" : 0 }

CityName value changed in c# :
item.ToJson()
"

{\"CityName\":\"Antalya\",\"IsSentToSql\":0,\"UserId\":\"20277B19-F603-4CA1-8B3A-B2A009BB291A-@o@-1E43F402-0A2E-4E11-9708-15F7AD9EFA82\",\"Email\":\"xxxxxx@hotmail.com\",\"FirstName\":\"xxxx\",\"LastName\":\"yyyyy\",\"BirthDate\":\"\\/Date(612824400000+0300)\\/\",\"Password\":\"test\",\"Gender\":0,\"CityId\":7,\"TownId\":0,\"IsDisabled\":1,\"IsGlobalDisabled\":0,\"IsRegKeySent\":-1,\"IsRegKeyValid\":-1,\"Id\":1,\"ParishId\":0}

"

After update the record ( without Id criteria in query):
> db.user.find({_id:1})

{ "_id" : NumberLong(1), "UserId" : "20277B19-F603-4CA1-8B3A-B2A009BB291A-@o@-1E43F402-0A2E-4E11-9708-15F7AD9EFA82", "Email" : "xxxxxx@hotmail.com", "FirstName" : "xxxx", "LastName" : "yyyyy", "BirthDate" : ISODate("1989-06-02T21:00:00Z"), "Password" : "test", "Gender" : 0, "CityId" : 7, "TownId" : 0, "IsDisabled" : 1, "IsGlobalDisabled" : 0, "IsRegKeySent" : -1, "IsRegKeyValid" : -1, "LastSeenOn" : null, "ParentUserId" : null, "RegisteredOn" : null, "ValidatedOn" : null, "ParishId" : 0, "CityName" : "Antalya", "TownName" : null, "ParishName" : null, "IsSentToSql" : 0 }
Comment by Robert Stam [ 19/Jul/12 ]

From the error message it looks like the duplicate key is on the _id field with a value of 1. You can display this document in the mongo shell using:

> db.coll.findOne({_id:1})

Replace "coll" with the actual name of your collection.

If you could provide the document as displayed by the mongo shell, as well as setting a breakpoint in Visual Studio on your call to Update and type item.ToJson() in the Visual Studio immediate window and report that back as well.

Comment by erhan [ 18/Jul/12 ]

Hello , I could not gather any error msg by using mongo shell but I've copied the exception msg generated by C# below.
And I modify 3 different fields before update.These fields are not index fields.

MongoDB.Driver.MongoSafeModeException was unhandled
Message=Safemode detected an error 'E11000 duplicate key error index: test.user.$id dup key: { : 1 }'. (Response was { "err" : "E11000 duplicate key error index: test.user.$id dup key: { : 1 }", "code" : 11000, "n" : 0, "connectionId" : 183, "ok" : 1.0 }).
Source=MongoDB.Driver
StackTrace:
at MongoDB.Driver.Internal.MongoConnection.SendMessage(MongoRequestMessage message, SafeMode safeMode) in C:\source\mongo-csharp-driver\Driver\Internal\MongoConnection.cs:line 516
at MongoDB.Driver.MongoCollection.Update(IMongoQuery query, IMongoUpdate update, MongoUpdateOptions options) in C:\source\mongo-csharp-driver\Driver\Core\MongoCollection.cs:line 1512
at MongoDB.Driver.MongoCollection.Update(IMongoQuery query, IMongoUpdate update, UpdateFlags flags) in C:\source\mongo-csharp-driver\Driver\Core\MongoCollection.cs:line 1544
at Sso.DataEngine.UserEngine.Start() in D:\Repository\clients\test\Sso\Sso.DataEngine\UserEngine.cs:line 70
at Sso.DataEngine.EngineBase.ToMongoDB(String[] args) in D:\Repository\clients\test\Sso\Sso.DataEngine\EngineBase.cs:line 48
at Sso.DataEngine.Program.Main(String[] args) in D:\Repository\clients\test\Sso\Sso.DataEngine\Program.cs:line 38
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

Comment by Robert Stam [ 17/Jul/12 ]

It would help if you could provide the document (using the mongo shell) that your are getting the exception with as well as the full error message. Does the full error message tell you which index is the cause of the duplicate key?

Are you modifying any values of your item variable before calling Update?

Comment by erhan [ 17/Jul/12 ]

PS:I have unique indexes on Email and UserId.

Generated at Wed Feb 07 21:37:06 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.