1111013
(Антон Гридюшко)
February 7, 2022, 2:25pm
#1
Have an api like:
var filter = Builders<Deal>.Filter.Eq("_id", offerPlusModel.DealId);
var update = Builders<Deal>.Update
.AddToSet(x => x.Offers, offerToCreate)
.Set("LastModified", DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
await _unitOfWork.GetCollection<Deal>("Deal").UpdateOneAsync(filter, update);
var results = _unitOfWork.GetCollection<Deal>("Deal")
.Aggregate()
.Lookup(
"UserProfile",
"offers.clientId",
"_id",
@as: "dealWithOffersWithClients")
.ToListAsync().Result;
This first creates object by update, then from this object searches by clientId additional information for client.
Works fine, but firstly, I want only one request, secondly, I need just some fields from the client model in UserProfile table.
Ok, how to concatenate in one request?
1111013
(Антон Гридюшко)
February 7, 2022, 2:46pm
#2
Structure of table Deal:
_id:"f9097a32-23a7-4242-b34d-1a6d54c17e14"
offers:Array
0:Object
_id:"5623524d-10a5-45ed-8a82-b5e9ae62d893"
parcelId:"330c2c46-d2d9-4ba4-8471-8510bc8c100e"
clientId:"3f7d416f-879d-41be-b411-2a08db977aa6"
totalSum:"0"
description:null
dealStatus:3
lastModified:"Friday, 04 February 2022 21:40:05"
shifterId:"3f7d416f-879d-41be-b411-2a08db977aa6"
1111013
(Антон Гридюшко)
February 7, 2022, 3:02pm
#3
Also I would like to obtain not all aggreagated documents, but only for the deal, that I updated.
1111013
(Антон Гридюшко)
February 7, 2022, 4:01pm
#4
Ok, I have now that I want, but again in two queries.
var filter = Builders<Deal>.Filter.Eq("_id", offerPlusModel.DealId);
var update = Builders<Deal>.Update
.AddToSet(x => x.Offers, offerToCreate)
.Set("LastModified", DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
await _unitOfWork.GetCollection<Deal>("Deal").UpdateOneAsync(filter, update);
var results = _unitOfWork.GetCollection<Deal>("Deal")
.Aggregate()
.Lookup(
"UserProfile",
"offers.clientId",
"_id",
@as: "dealWithOffersWithClients")
.Project(Builders<BsonDocument>.Projection
.Exclude("dealWithOffersWithClients._id")
.Exclude("dealWithOffersWithClients.createdOn")
.Exclude("dealWithOffersWithClients.thirdName")
.Exclude("dealWithOffersWithClients.birthday")
.Exclude("dealWithOffersWithClients.phoneNumber")
.Exclude("dealWithOffersWithClients.phoneNumberConfirmed")
.Exclude("dealWithOffersWithClients.email")
.Exclude("dealWithOffersWithClients.emailConfirmed")
.Exclude("dealWithOffersWithClients.active")
.Exclude("dealWithOffersWithClients.isNew")
.Exclude("dealWithOffersWithClients.password")
.Exclude("dealWithOffersWithClients.smsCode")
.Exclude("dealWithOffersWithClients.fcmToken")
.Exclude("dealWithOffersWithClients.smsCode")
.Exclude("dealWithOffersWithClients.claims")
)
.Match(new BsonDocument { { "_id", offerPlusModel.DealId.ToString() } })
.ToListAsync().Result;