1

I don't know if its just me but I've found the documentation for the mongodb c# driver to be so limited. Every time I try to do something more than a basic get/insert I end up in trial and error mode...

So I have a json document looks roughly something like ...

{
   Year: "2015",
   Vehicles : [
      VehicleType : "Trucks",
         Makes: [
             {
               Name: "Ford",
               Models: 
               [
                  {
                      Name: "F150",
                      Sales: 
                      [
                         {
                            Ref: FORDTRUCK01,
                            Month: 01,
                            Sold: 100,
                            Total: $10,000   
                         },
                         {
                            Ref: FORDTRUCK02
                            Month: 02,
                            Sold: 150,
                            Total: $12,000   
                         }
                      ]
                   },
                   {
                      Name: "F350",
                      Sales: 
                      [
                         {
                            Ref: FORDTRUCK03,
                            Month: 01,
                            Sold: 100,
                            Total: $10,000   
                         },
                         {
                            Ref: FORDTRUCK04
                            Month: 02,
                            Sold: 150,
                            Total: $12,000   
                         }
                      ]
                   }
               ]
           }
        ]
      ]
   },
   {
      Year: "2014",
      Vehicles : [
      VehicleType : "Trucks",
         Makes: [
             {
               Name: "Ford",
               Models: 
               [
                   {
                      Name: "F250",
                      Sales: 
                      [
                         {
                            Ref: FORDTRUCK01,
                           Month: 01,
                            Sold: 100,
                            Total: $10,000   
                         },
                         {
                            Ref: FORDTRUCK02
                            Month: 02,
                            Sold: 150,
                            Total: $12,000   
                         }
                    },
                    {
                      Name: "F150",
                      Sales: 
                      [
                         {
                            Ref: FORDTRUCK03,
                            Month: 01,
                            Sold: 100,
                            Total: $10,000   
                         },
                         {
                            Ref: FORDTRUCK04
                            Month: 02,
                            Sold: 150,
                            Total: $12,000   
                         }
                    }
                 ]
               ]
           }
        ]
      ]
   }

All I want to do is delete the embedded sales document for

Ref: FORDTRUCK01 where Year is 2015

without having to pass the entire document back and forth - this is a contrived example of what is actually a much larger document I don't see the need to either re-get the whole document ... or edit the document on the client and pass the entire thing back when all I want to do is delete 4 fields (one embedded document) based on these two parameters. so this is my hodgepodge of code from other SO posts I found - Im fairly certain my issue is with the first parameter of the PullFilter - but I can't seem to find an example that is deeper than one level of the bsondocument

var client = new MongoClient(connection);
var db = client.GetDatabase("db");
var collection = db.GetCollection<BsonDocument>("Vehicles");
BsonDocument filter = new BsonDocument("Year", year);
BsonDocument update = Builders<SalesYear>.Update.PullFilter("Vehicles.Makes.Models.Sales", Builders<SalesMonth>.Filter.Eq("Ref", "FORDTRUCK01")).ToBsonDocument();

var result = collection.FindOneAndUpdateAsync(filter, update).Result;

I keep getting an exception

{"Element name '_t' is not valid'."}

Any help greatly appreciated. If anyone could point me to good .net resources for the c# 2.0 driver would also be greatly appreciated

maehue
  • 505
  • 10
  • 26
  • Maybe not what you want to hear, but you'll have to physically recreate the file if you want to remove bytes from its contents. Files just don't "eat up" parts of themselves, the layout is static once on disk. So regardless of the way you do this update, there *will* be a full read to memory and a rewrite back to disk. – Blindy Jun 22 '15 at 13:17
  • @Blindy: But even if the server has to load it all into memory that is a cheap operation compared to transmitting it over the network to whatever client it is and back again. – Chris Jun 22 '15 at 13:32
  • @MigoFast: Is `PullFilter` a standard part of the driver you are using or is that one of your own methods? I can't actually seem to find it and likewise can't find `Builders` in my drivers either so can't follow that to find the code you are running... – Chris Jun 22 '15 at 14:16
  • Yes I found it with the post http://stackoverflow.com/questions/30141958/mongodb-net-driver-2-0-pull-remove-element – maehue Jun 22 '15 at 14:20
  • Ah, looks like I'm on an older version of the drivers will be why I couldn't see it! Means I'm not sure I can help, sadly. :( – Chris Jun 22 '15 at 16:01
  • Though having relooked its claiming that `_t` is not valid which is what it uses to determine the c# type associated with a document or subdocument. I notice that your document had no _t values in it anywhere so maybe when you are referencing `SalesMonth` in your filter builder it is having a problem that there is no type information on the document. Just a random thought of a place to start looking... – Chris Jun 22 '15 at 16:05
  • 1
    Yea, thats part of my frustration - the newer driver is so different that most older posts don't exhibit the 'new' way - I know there is backwards compatibility but their .net documentation is on par with all msdn standard documentation - which is to say if they tell you the function/property is there - it is properly documented - never mind how to actually use it – maehue Jun 22 '15 at 16:11

1 Answers1

1

I've been working with PullFilter a lot in my own code recently, so here is my suggestion:

var update = Builders<BsonDocument>.Update.PullFilter(
    c => c.Vehicles.Makes.Models.Sales, 
    s => s.Ref == "FORDTRUCK01");

var result = col.FindOneAndUpdate(c => c.Year == "2015", update);
Ray
  • 67
  • 7
  • 1
    BsonDocument doesn't have field Vehicles.Makes.Models.Sales. You need to specify strongly typed Entity in the generic parameter. – Prabhakar Apr 15 '19 at 04:09