C # MongoDb BsonDocument Add subdocument with a new ObjectId

1

Starting from:

Class that adds a subdocument to a document

   public Boolean AggregateSubdocument(string nameCollection, string idDocument, string nameSubdocument, object subdomcument)
    {

        BsonDocument newSubdocument = BsonDocument.Parse(JsonConvert.SerializeObject(subdomcument));
        newSubdocument.Remove("Id");
        var collection = db.GetCollection<BsonDocument>(nameCollection);
        var filter = Builders<BsonDocument>.Filter.Eq("_id", ObjectId.Parse(idDocument));
        var update = Builders<BsonDocument>.Update.Push(nameSubdocument, newSubdocument);
        var result = collection.UpdateOne(filter, update);
        if (result.IsModifiedCountAvailable)
        {
            if (result.ModifiedCount == 1)
            {
                return true;
            }
        }
        return false;
    }

It generates the following:

Inside my collection

  "_id" : ObjectId("59f3540a91bfb21a40121a17"),
    "Nombre" : "el  conde dracula",
    "Viviendas" : "1",
    "Estado" : "condesco",
    "Ciudad" : "transilvania",
    "Colonia" : "conde",
    "Cp" : "20398",
    "Direccion" : "tres tumbas, calle murcielago",
    "Correo" : "[email protected]",
    "Referencia" : "cerca de los hombres lobo",
    "Usuarios" : [
            {
                    "Nick" : "59f3540a91bfb21a40121a17",
                    "Nombre" : "sr. dracula",
                    "Tipo" : "administrador",
                    "Email" : "[email protected]",
                    "Password" : "jy2u9wcwofklm8rpef38xikun5dvffgwjsnbuq4btepfwlymoxyl0b8dcyftzwgt",
                    "Estado" : "codesco",
                    "Ciudad" : "transilvania",
                    "Cp" : "209",
                    "Direccion" : "calle vampiro, s/n"
            },
            {
                    "Nick" : "59f3540a91bfb21a40121a17",
                    "Nombre" : "usuario dos",
                    "Tipo" : "normal",
                    "Email" : "[email protected]",
                    "Password" : "zlzf8tmd0hbjeeahx+ucqbrehiycatqnkhqexcrkdeyo7np0kp/ot3ek0r9sxqxa",
                    "Estado" : "test",
                    "Ciudad" : "test",
                    "Cp" : "23255",
                    "Direccion" : "test"
            }
    ]

With the line

var update = Builders<BsonDocument>.Update.Push(nameSubdocument, newSubdocument);

I manage to generate an array, in which you can add n nodes, but you do not generate a "_id" : ObjectId() in each node

Question

How to generate an ObjectID () in each subcollection?

    
asked by harriroot 27.10.2017 в 19:04
source

2 answers

1

A solution

var update = Builders<BsonDocument>.Update.AddToSet(nameSubdocument, new BsonDocument(){
            {"_id", ObjectId.GenerateNewId()},
            newSubdocument
        });

Creation of a new document

new BsonDocument(){}

Inside the keys you can create "n" elements for the example document

new BsonDocument(){
            {"_id", ObjectId.GenerateNewId()},
            {"campo1", "valor1"},
            {"campon", 2}

        }

use the variable "newSubdocument", which already brings all those n elements that I need

    
answered by 27.10.2017 / 20:32
source
1
String _id=ObjectId.GenerateNewId().ToString();
newSubdocument.add(new BsonElement("_id",_id));

right after: newSubdocument.Remove ("Id");

I hope you serve, greetings

    
answered by 27.10.2017 в 20:06