[英]How to get the id of a document using c# driver mongodb
I need to take the id from a document to put it into another one, but i can't achieve this. 我需要将文档中的ID放入另一个文档中,但是我无法实现这一点。 I've tried many times but i get bad results.
我已经尝试了很多次,但结果却很糟糕。 If someone could help.
如果有人可以帮忙。
Here is my controller : 这是我的控制器:
public ActionResult LancerComparaison(ResultViewModel resultViewModel)
{
if (resultViewModel!= null)
{
UserInformationViewModel info = resultViewModel.UserInformationViewModel;
var client = new MongoClient("mongodb://localhost:27017");
var objDatabase = client.GetDatabase("Test");
var collection = objDatabase.GetCollection<BsonDocument>("UsersInformations");
BsonDocument objDocument = new BsonDocument {
{"Nom",info.NomUser},
{"Prenom",info.PrenomUser},
{"Email",info.EmailUser},
};
collection.InsertOne(objDocument);
var id = objDocument.{_id}
List<ObjetTransfert> listePrestationsObjetsTransferts = new List<ObjetTransfert>();
ResultatComparaison resultatComparaison;
ConvertGViewModelToGBd(resultViewModel, listePrestationsObjetsTransferts);
_compaSBT.ComparerP(listePrestationsObjetsTransferts, out resultatComparaison,
Enums.E.T);
//Enregistrer d'abord le resultat en Bd
resultatComparaison.IdentifiantResultatComparaison = new Guid().ToString();
resultatComparaison.IdentifiantUserInformation = id.ToString();
_resultatComparaisonService.Create(resultatComparaison);
Session["IdentifiantResultatComparaison"] = resultatComparaison.Id;
return View();
}
return null;
}
when you save new document your Bson Id is set inside the same document passed by InsertOne method after the execution. 当您保存新文档时,您的Bson ID会在执行后通过InsertOne方法传递的同一文档中设置。
collection.InsertOne(objDocument);
var id = objDocument.{yourBsonIdProp}
I hope this helps you. 我希望这可以帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.