0

In my import function, i am replacing existing documents or adding new ones using the Upsert option:

  var builder = Builders<FacilityDocument>.Filter;
  var filter = builder.Eq(x => x.Language.LCID, lcid)
               & builder.Eq(x => x.Name, facility.Name)
               & builder.Eq(x => x.NameDetailed, facility.NameDetailed)
               & builder.Gte(x => x.ImportedDate, new DateTime(DateTime.Now.Year, 1, 1));

  collection.ReplaceOne(filter, facility, new UpdateOptions { IsUpsert = upsert });

Now in my POCO class, i have it configured to use GUID instead of an ObjectID, like this:

[BsonId]
[BsonIgnoreIfDefault]
public Guid ID { get; set; }

The problem is that in the database, instead of generating a GUID, its defaulting back to ObjectId:

mongodb issue print

And what it should be doing, is this (which works when using the regular Insert method):

mongodb how it should be

Much appreciated if anyone have a solution for this.

Update

This is my entire facilitydocument class:

[BsonId]
[BsonIgnoreIfDefault]
public Guid ID { get; set; }
public string Name { get; set; }
public string NameDetailed { get; set; }
public CommuneDocument Commune { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public string Homepage { get; set; }
public FacilityTypeDocument FacilityType { get; set; }
public FacilityPlacement FacilityPlacement { get; set; }
public int CoursesCount { get; set; }
public CoursesData[] CoursesData { get; set; }
public OwnershipDocument Ownership { get; set; }
public OperationDocument Operation { get; set; }
public string ExternalRemarks { get; set; }
public GisLocationData GisLocationData { get; set; }
public string ContactPersonName { get; set; }
public string ContactPersonEmail { get; set; }
public string InternalRemarks { get; set; }
public bool Active { get; set; }
public LanguageDocument Language { get; set; }
public DateTime ImportedDate { get; set; }
public string ImportedBy { get; set; }
public string UpdatedBy { get; set; }
public DateTime UpdatedDate { get; set; }
public List<ChangeLog> ChangeLog { get; set; }

Update 2

If i set my ID before the replace method, i get an error like this:

A write operation resulted in an error. The _id field cannot be changed from {_id: BinData(3, BF515DEF5743F547BD3EABB1A89DAC4D)} to {_id: BinData(3, 6364DF16640A4346B62E6B866BF76069)}

This is how i set it:

    facilityDocument.ID = Guid.NewGuid();
mp1990
  • 9,699
  • 3
  • 13
  • 17
  • How does data fields in facility class code look like ? – s7vr Feb 22 '17 at 10:45
  • You need to set the ID before you call ReplaceOne – Toan Nguyen Feb 22 '17 at 10:49
  • Okay I meant what all fields are you setting. Are you setting the id field in the update or filter ? – s7vr Feb 22 '17 at 10:50
  • you couldn't change your id. I would suggest to insert new items and to delete old items. Look for example here similar question: http://stackoverflow.com/questions/4012855/how-update-the-id-of-one-mongodb-document – Maksim Simkin Feb 22 '17 at 10:58
  • @MaksimSimkin thank you. it just seems like a bug, because it all works fine except that when it creates a new document it should generate a guid like the regular insert function, instead of an objectid. deleting documents in order to replace them seems like a bad workaround, specially since its a possibility that the document id could be referenced somewhere. – mp1990 Feb 22 '17 at 11:32
  • Take a look at this answer. http://stackoverflow.com/questions/22943670/mongodb-c-sharp-upsert-with-guid. – s7vr Feb 22 '17 at 11:44
  • @Veeram would definetly be the way to go. Except the production mongo server version is currently 2.2, and it has to be 2.6 or newer for the solution in your link to work. Im in the process to see if an mongo upgrade is the possibility. I would mark your comment as the answer if i could. – mp1990 Feb 22 '17 at 12:07
  • @Veeram theres another hatch though. The issue described in your link is somewhat similar, but the solution is for the update method. I need to replace all the properties, instead of updating each one manually. And there is no Update definition for replace, so i cannot use SetOnInsert. – mp1990 Feb 22 '17 at 12:17

0 Answers0