mongodb - Mongo replace causing duplicates in sub documents c# -


interface nameable {     string name { get; set; } }  class parent : nameable {     public string name { get; set; }      public list<child> children { get; set; } = new list<child>(); }  class child {      public string name { get; set; }      public int value { get; set; }      public string dataone { get; set; }      public string datatwo { get; set; }      public double datathree { get; set; } }        static async void mainasync(string[] args)     {         (int = 0; < random.next(10000, 50000); i++)         {             parents.add(createparent());         }          parents = parents.groupby(g => g.name).select(grp => grp.first()).tolist();          foreach (var parent in parents)         {             await insert<parent>(parent);         }          // update objects randomly;          foreach (var parent in parents)         {             (int = 0; < random.next(10, 30); i++)             {                 int decision = random.next(0, 2);                  if (decision == 0 && parent.children.count > 0)                 {                     parent.children.removeat(random.next(0, parent.children.count));                 }                 else                 {                     var inner = createchild();                     if (!parent.children.any(io => io.name == inner.name))                     {                         parent.children.add(inner);                     }                  }                  await replaceone<parent>(parent);             }                      }     } 

i have list of parents , each 1 contains list of child elements. when using c# mongo driver replace these parents after have been updated either removing or adding new children creates duplicates of child on mongo side despite there being no duplicates when code calls replace method.

i think atomic sub document structure of mongo , how updates/replaces items. there way prevent creating duplicates? , if not happening due atomic nature causing this? edit:

    static async task replaceone<t>(t obj)         t : nameable     {         await database.getcollection<t>(typeof(t).name).replaceoneasync(builders<t>.filter.where(t => t.name == obj.name), obj);     }      static async task insert<t>(t obj)     {         await database.getcollection<t>(typeof(t).name).insertoneasync(obj);     }      static parent createparent()     {         var innerobjects = new list<child>();         (int = 0; > random.next(1, 10); i++)         {             innerobjects.add(createchild());         }          return new parent()         {             name = randomstring(),             children = innerobjects         };     }      static child createchild()     {         return new child()         {             name = randomstring(),             value = randomint(),             dataone = randomstring(),             datatwo = randomstring(),             datathree = randomdouble()         };     } 

added replace/insert snippets, using mongo c# driver insert db. createparent , createchild fills objects random relevant data.

i tried guess randomstring(), randomint() , randomdouble() methods , ran project several times without cleaning database. not detect duplicates whatsoever based on 2 "name" properties (on parent , child).

i suspect observation somehow incorrect. in order check if have duplicate children within same parent can use following query:

collection.aggregate( {     $unwind: "$children" }, {     $group:     {         _id:         {             "name": "$name",             "childname": "$children.name"         }         , "count": { $sum: 1 }     } }, {     $match:     {         "count": { $ne: 1 } }     } ) 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -