private void button2_Click(object sender, EventArgs e) { var connectionString = "mongodb://localhost/?safe=true"; var server = MongoServer.Create(connectionString); var db = server.GetDatabase("CookingBook"); var kuvariCollection = db.GetCollection <Kuvar>("kuvari"); var collection = db.GetCollection <Recepts>("Recepts"); var i = 1; dataGridView1.Rows.Clear(); //svi radnici osim prva 2 rade u sektoru 1 /* foreach (Recepts r in collection.FindAll()) * { * kuvar.Recepts.Add(new MongoDBRef("Recepts", kuvar.Id)); * * r.radnik = new MongoDBRef("kuvari", r.Id); * * collection.Save(r); * kuvariCollection.Save(kuvar); * }*/ var query = Query.And( (Query.EQ("ID_Kartice", kuvar.ID_Kartice.ToString())), Query.EQ("sifra", kuvar.sifra.ToString()) ); var sad = kuvariCollection.Find(query); if (sad.Count() == 1) { foreach (Kuvar r in sad) { foreach (MongoDBRef radnikRef in r.Recepts) { Recepts recept = db.FetchDBRefAs <Recepts>(radnikRef); DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone(); row.Cells[0].Value = i; row.Cells[1].Value = recept.naziv; row.Cells[2].Value = recept.Id; dataGridView1.Rows.Add(row); i++; } } } }
private void button1_Click(object sender, EventArgs e) { BsonValue OriginalImageId = null; if (Id == String.Empty) { if (sFileName == null && !File.Exists(sFileName)) { MessageBox.Show("Please select image"); } else { var connectionString = "mongodb://localhost/?safe=true"; var server = MongoServer.Create(connectionString); var db = server.GetDatabase("CookingBook"); var kuvariCollection = db.GetCollection <Kuvar>("kuvari"); var collection = db.GetCollection <Recepts>("Recepts"); Recepts k = new Recepts(); k.opis = opis.Text.ToString(); k.naziv = naziv.Text.ToString(); k.zacini = zacini.Text.ToString(); k.vreme = vreme.Text.ToString(); k.dodaci = dodaci.Text.ToString(); k.ImageId = fileId; collection.Insert(k); var query = Query.And( (Query.EQ("ID_Kartice", kuvar.ID_Kartice)), Query.EQ("sifra", kuvar.sifra) ); var sad = kuvariCollection.Find(query); foreach (Kuvar r in sad) { MongoDBRef referenca = new MongoDBRef("Recepts", k.Id); r.Recepts.Add(referenca); k.radnik = new MongoDBRef("kuvari", r.Id); collection.Save(k); kuvariCollection.Save(r); } this.Close(); } } else { var connectionString = "mongodb://localhost/?safe=true"; var server = MongoServer.Create(connectionString); var db = server.GetDatabase("CookingBook"); var kuvariCollection = db.GetCollection <Kuvar>("kuvari"); var collection = db.GetCollection <Recepts>("Recepts"); ObjectId objectId = new ObjectId(Id); var query = Query.And( (Query.EQ("_id", objectId)) ); var recept = collection.Find(query); string OriginalImageName = null; bool takeOriginal = true; if (recept.Count() == 1) { foreach (Recepts r in recept) { ObjectId oid = new ObjectId(r.ImageId.ToString()); OriginalImage = db.GridFS.FindOne(Query.EQ("_id", oid)); OriginalImageName = OriginalImage.Name; OriginalImageId = OriginalImage.Id; } } if (UploadedFile == null) { UploadedFile = OriginalImage; } sFileName = newFileName + "\\" + System.IO.Path.GetFileName(UploadedFile.Name); var sFileNameOriginal = newFileName + "\\" + System.IO.Path.GetFileName(OriginalImageName); if (sFileName != sFileNameOriginal) { takeOriginal = false; } Recepts k = new Recepts(); k.opis = opis.Text.ToString(); k.naziv = naziv.Text.ToString(); k.zacini = zacini.Text.ToString(); k.vreme = vreme.Text.ToString(); k.dodaci = dodaci.Text.ToString(); k.ImageId = takeOriginal? OriginalImageId : fileId; List <UpdateBuilder> list = new List <UpdateBuilder>(); var opis_update = MongoDB.Driver.Builders.Update.Set("opis", BsonValue.Create(k.opis)); var naziv_update = MongoDB.Driver.Builders.Update.Set("naziv", BsonValue.Create(k.naziv)); var zacini_update = MongoDB.Driver.Builders.Update.Set("zacini", BsonValue.Create(k.zacini)); var vreme_update = MongoDB.Driver.Builders.Update.Set("vreme", BsonValue.Create(k.vreme)); var dodaci_update = MongoDB.Driver.Builders.Update.Set("dodaci", BsonValue.Create(k.dodaci)); var slika_update = MongoDB.Driver.Builders.Update.Set("ImageId", k.ImageId); list.Add(opis_update); list.Add(naziv_update); list.Add(zacini_update); list.Add(vreme_update); list.Add(dodaci_update); list.Add(slika_update); var updateCombine = MongoDB.Driver.Builders.Update.Combine(list); collection.Update(query, updateCombine); foreach (Recepts r in collection.Find(query)) { string sadsd = r.naziv; } this.Close(); } }