Querying Array Elements by DBRef for Deletion

Hello. I’m still new to the nuances of Mongo and have the following issue. I’m working on a .Net project using MongoDB.Driver 2.13.0. I have the following document structure.

I have an “lrec80” array with one or more elements that contain a “RR4NFAD” DBRef to a document in another collection.

{
    "lrec80" : [ 
        {
            "RR4NFAD" : {
                "$ref" : "RR4OCO",
                "$id" : ObjectId("0000000000000000879866fe"),
                "$db" : "tpfdf"
            },
            "RR4NCTY" : "LAX",
            "RR4NNBR" : 1
        }, 
        {
            "RR4NFAD" : {
                "$ref" : "RR4OCO",
                "$id" : ObjectId("0000000000000000879866fe"),
                "$db" : "tpfdf"
            },
            "RR4NCTY" : "MSY",
            "RR4NNBR" : 1
        }, 
    ],
    "_id" : ObjectId("0000000000000000ec013401")
}

I can easily delete the referenced document in the other collection, but how do I perform a batch delete of elements in the array that all have the same ObjectID of the document in the other collection?

I’ve tried the following without success to filter out the elements from the array for deletion

                var mgdbref = new MongoDBRef(_appConfiguration.MongoDbConnection["MongoDatabaseName"],
                    "RR4OCO", "0000000000000000ec013401");

                var embeddedDoc = new BsonDocument
                {
                    /*{ "$ref", "RR4OCO"},*/
                    { "$id", new ObjectId(objectId) }
                };

                var deleteDoc = new BsonDocument
                {
                    { "RR4NFAD", embeddedDoc (or mgdbref)  }
                };

I’m probably missing something simple, but I can’t figure it out. Thanks for any help you can offer!

Didn’t include the other part. I tried to filter out the elements for deletion, with the following:

                var deleteReadFilter = Builders<BsonDocument>.Filter.Eq("_id", id) &
                    Builders<BsonDocument>.Filter.ElemMatch<BsonValue>("lrec80", embeddedDoc);

                var document = (await _mongoCollection.FindAsync(deleteReadFilter).ConfigureAwait(false)).ToList();

                if (document != null && document.Any())
                {
                    // Delete existing record
                    var deleteFilter = Builders<BsonDocument>.Update.Pull("lrec80", embeddedDoc);
                    UpdateResult updateResult = _mongoCollection.UpdateOne(readFilter, deleteFilter, new UpdateOptions() { BypassDocumentValidation = true });
                    retVal = (updateResult.IsAcknowledged && updateResult.ModifiedCount > 0);

I get the following error:

QueryFailure flag was true (response was { “$err” : “MONG0025E 13.37.37 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.” }).

Welcome to the MongoDB Community @RKS !

This server response is not a MongoDB server error message, so I assume you are using an emulated API that has some different expectations like “Reference must be set using a DBRef that contains an ObjectID”. You would have to consult the relevant API documentation for more guidance.

What type of deployment are you connecting to?

Regards,
Stennie

Hi, you are correct that part of the error is non-standard and specific to our environment. The “Reference must be set using a DBRef that contains an ObjectID” was what I was concerned about.

The service I’m working on is .Net Core using the official .Net driver for MongoDB connecting to MongoDB version 2.6.5 in a z/TPF environment version 1.1.

If I can query and delete one or more array elements from the “lrec80” array where RR4NFAD is a DBRef to a document in another collection, that would resolve the issue I’m facing. For my example, I would take ObjectId “0000000000000000879866fe” as input and then delete array elements with RR4NFAD.$id that match.

Thanks for your time.

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("0000000000000000879866fe") }
};

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

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

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.