1

I've just started working with MongoDB for c# and I'm trying to upsert some documents in a collection. I'm using the 'save' command (and even tried update with upsert flag on) but every time I run my code it keeps inserting duplicates: the new records have new object ids (randomly generated) but all the rest of the data is the same. What am I doing wrong? Any suggestion is greatly appreciated.

Here's my code:

List<LatestDataReduced> latestdata = new List<LatestDataReduced>(); 
//LatestDataReduced is the model of my documents
foreach (var dep in lrd) 
{
    foreach (var rec in dep.record)
    {
        var entity = new LatestDataReduced();
        entity.PlatformID = platid;

        //fill up data in entity...    

        latestdata.Add(entity);
    }
}
var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase("emodnet2");
var collection = database.GetCollection<LatestDataReduced>("latestdata");
foreach (var ld in latestdata)
{
    /*var query = Query.And(Query.EQ("Id", ld.Id), Query.EQ("Date", ld.Date), Query.EQ("Depth", ld.Depth), Query.EQ("PlatformID", ld.PlatformID), Query.EQ("Pressure", ld.Pressure));
    var update = Update.Replace(ld);
    collection.Update(query, update, UpdateFlags.Upsert);*/

    collection.Save(ld);
}

LatestDataReduced class:

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MongoDB.Models
{
    public class CodeValuePair
    {
        public string Code { get; set; }
        public double Value { get; set; }
    }

    public class LatestDataReduced
    {
        //[BsonIgnoreIfDefault]
        public ObjectId Id { get; set; }
        public int PlatformID { get; set; }
        public DateTime Date { get; set; }
        public double Depth { get; set; }
        public List<CodeValuePair> ParamValue { get; set; }
        public double Pressure { get; set; }
        public List<CodeValuePair> ParamValueInfo { get; set; }
        public string Roos { get; set; }
    }
}
4
  • Can you edit your question to show the definition for LatestDataReduced? If your LatestDataReduced class defines an Id property, then Mongo should be using that as the _id value in the saved docs. Commented Sep 26, 2014 at 16:35
  • It's defo mongo and not your 'event' firing twice? Commented Sep 26, 2014 at 16:38
  • @JohnnyHK yes, I am defining an Id, but I'm leaving it blank for MongoDB to randonmly generate one whenever I insert a new document. Maybe mongo does not consider two documents equal because they have different Ids? But in fact they are equal, so they are duplicates if mongo insert them both... Commented Sep 29, 2014 at 6:57
  • @PaulZahra I'm physically running this twice: what I want is that the documents which are equal get updated instead of inserted - I understand that this should be the right behaviour for "save", isn't it? Commented Sep 29, 2014 at 6:58

1 Answer 1

3

I found a way to make this work!

I thought it was mandatory to use the class "ObjectId" for Ids in mongo and I was not able to give a real identifier to my documents, but then I found out you can simply use a String Id:

        public class LatestDataReduced
        {
            //[BsonIgnoreIfDefault]
            public String Id { get; set; }
            public int PlatformID { get; set; }
            public DateTime Date { get; set; }
            public double Depth { get; set; }
            public List<CodeValuePair> ParamValue { get; set; }
            public double Pressure { get; set; }
            public List<CodeValuePair> ParamValueInfo { get; set; }
            public string Roos { get; set; }
        }

so I gave my documents a valid identifier and mongo is now able to recognize equal documents correctly and update them instead of inserting them.

Thank you very much for your attention!

Have a nice monday

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.