0

Using Asp.Net Core and MongoDB.Driver 2.13.0. I have a document (object id = 0000000000000000ec013401) with the following structure: an "lrec80" array of elements, the "RR4NFAD" property of array element contains an reference to a document in another collection.

{
"_id" : ObjectId("0000000000000000ec013401"),
"lrec80" : [ 
{
"RR4NFAD" : {
    "$ref" : "RR4OCO",
    "$id" : ObjectId("0000000000000000879866fd"),
    "$db" : "tpfdf"
},
"RR4NCTY" : "LAX"
}, 
{
"RR4NFAD" : {
    "$ref" : "RR4OCO",
    "$id" : ObjectId("0000000000000000879866fd"),
    "$db" : "tpfdf"
},
"RR4NCTY" : "SFO",
}}

I can delete the document in the other RR4OCO collection without any issue, but how do I delete one or more elements from the lrec80 array that have object ID references to that other deleted document?

For example, how would I remove elements from array lrec80 that have object IDs of "0000000000000000879866fd"?

I've tried the following without success:

            var id = new ObjectId("0000000000000000ec013401");
        var readFilter = Builders<BsonDocument>.Filter.Eq("_id", id);
  var idToDelete =  ObjectId.Parse("0000000000000000879866fd");
  var deleteDoc = new BsonDocument {
                { "$id", idToDelete },
                { "$ref", "RR4OCO" },
                { "$db", "tpfdf" }
            };

            var deleteDoc2 =  
                new BsonDocument
                {
                    { "RR4NFAD", deleteDoc }
                } 
            ;

  var deleteFilter = Builders<BsonDocument>.Update.Pull("lrec80", deleteDoc2);
  _mongoCollection.UpdateOne(readFilter, deleteFilter, new UpdateOptions() { BypassDocumentValidation = true });

The error I get is:

QueryFailure flag was true (response was { "$err" : "MONG0025E 13.38.38 An error occurred when a request that contains '/PrefixedLREC/DFLREC/lrec80/RR4NFAD' was processed. Reason: Reference must be set using a DBRef that contains an ObjectID. Path name: /PrefixedLREC/DFLREC/lrec80/RR4NFAD." }).

I realize the error contains some details specific to my environment, but I'm still curious in general, how to query and remove all the array elements by matching the RR4NFAD.$id ObjectId.

Thanks for any help you can provide.

RKS
  • 3
  • 4
  • Maybe this can be helpful: [remove element mongodb .net driver 2.0](https://stackoverflow.com/questions/30141958/mongodb-net-driver-2-0-pull-remove-element) – Lazar Đorđević Oct 12 '22 at 21:36
  • I try your code but unable to reproduce your error. From my side, I just get `UpsertedCount: 0`. What if you modify your `deleteDoc2` value as `new BsonDocument { { "RR4NFAD.$id", idToDelete } }`, does it work? – Yong Shun Oct 13 '22 at 01:11

1 Answers1

0

I managed to figure it out. As usual, I overcomplicated it. The following solution worked.

var readFilter = Builders<BsonDocument>.Filter.Eq("_id", new ObjectId("0000000000000000ec013401"));

var deleteDoc2 = new BsonDocument
{
    { "RR4NFAD", new ObjectId("0000000000000000879866fd") }
};

var deleteFilter = Builders<BsonDocument>.Update.Pull("lrec80", deleteDoc2);

_mongoCollection.UpdateOne(readFilter, deleteFilter, new UpdateOptions() { BypassDocumentValidation = true });
RKS
  • 3
  • 4