M
M
Michael2017-02-27 12:41:35
MongoDB
Michael, 2017-02-27 12:41:35

How to add fields to gridFS using C#?

Good afternoon!
How can I add additional fields to gridFS using C# ?
When saving the file, the userId field must be added to the standard document

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2017-02-28
@Slider_vm

Found this solution. Through the addition of Metadata.

var client = new MongoClient("mongodb://25.35.34.171:27017");
            var db = client.GetServer();
            var s = new MongoGridFSSettings();

            // чтение из файла
            using (FileStream fstream = File.OpenRead(@"C:\\Users\\Slide\\Pictures\\1268995974-dropdatabase.jpg"))
            {
                // преобразуем строку в байты
                byte[] array = new byte[fstream.Length];
                // считываем данные
                fstream.Read(array, 0, array.Length);

                var tt = new MongoGridFS(db, "ufanet", s);
                var cro = new MongoGridFSCreateOptions();
                cro.Metadata = new BsonDocument { { "name", "Bill" } };

                var f = tt.Upload(fstream,"dropdatabase.jpg",cro); 
            }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question