MongoDb 3.0 C# updating subdocument of subdocument - c#

Below is an example of my document. I am trying to update the CostReports part based on the id of the CostReportingPeriods element.
{
"_id" : "240106",
"CostReportingPeriods" : [
{
"FyBgnDt" : ISODate("2000-01-01T05:00:00.000Z"),
"FyEndDt" : ISODate("2000-12-31T05:00:00.000Z"),
"_id" : "240106-20000101-20001231",
"CostReports" : []
},
{
"FyBgnDt" : ISODate("2001-01-01T05:00:00.000Z"),
"FyEndDt" : ISODate("2001-12-31T05:00:00.000Z"),
"_id" : "240106-20010101-20011231",
"CostReports" : []
},
{
"FyBgnDt" : ISODate("2002-01-01T05:00:00.000Z"),
"FyEndDt" : ISODate("2002-12-31T05:00:00.000Z"),
"_id" : "240106-20020101-20021231",
"CostReports" : []
},
{
"FyBgnDt" : ISODate("2003-01-01T05:00:00.000Z"),
"FyEndDt" : ISODate("2003-12-31T05:00:00.000Z"),
"_id" : "240106-20030101-20031231",
"CostReports" : []
}
]
I am using the following code to try and update that element but am getting an error that says cannot use the element (CostReportingPeriods of CostReportingPeriods.CostReports) to traverse the element. If I add CostReportingPeriods.0.CostReports it will add it to the first element of the array, regardless of the filter.
var builder = Builders<MongoModels.Provider>.Filter;
var filter = builder.Eq("_id",costReport.PRVDR_NUM) & builder.Eq("CostReportingPeriods._id", costReport.UNIQUE_ID);
var update = Builders<MongoModels.Provider>.Update.AddToSet("CostReportingPeriods.CostReports", Mapper.Map<CostReport, MongoModels.CostReport>(costReport, opt =>
{
opt.AfterMap((src, dest) => dest.Worksheets = CreateWorksheets(dest.RptRecNum).ToList());
}));
How do I get it to update the element I want it to update based on the id of the subdocument?

After trying a bunch of different things, by channging my update filter from "CostReportingPeriods.CostReports" to "CostReportingPeriods.$.CostReports" it works flawlessly.

Related

Dynamically Query a Mongo Collection in c#

I'm new to C# mongo,earlier worked on Node and Mongo.
i have a collection called tasks.Below is a sample record.
{
"_id" : ObjectId("6193bfba23855443a127466a"),
"taskIdentifier" : LUUID("00000000-0000-0000-0000-000000000000"),
"title" : "PR Liquidators",
"company" : "iuytreugdfh",
"purpose" : "test purpose",
"column" : "Search",
"assignTo" : "Shiva",
"assignToId" : ObjectId("61933b47a79ac615648a7855"),
"assignToImage" : null,
"notes" : "ggh#William james ",
"done" : 0,
"taskID" : "00029",
"status" : "Pending",
"states" : [
"Alabama - AL",
"Alaska - AK"
],
"active" : true,
"updatedAtUtc" : ISODate("2021-11-18T12:26:37.616Z"),
"updatedBy" : ""
}
in my c# webapi Project i always get a array called filterCriteria from api request of below form:
filterCriteria=[
{key:"purpose",value:"test purpose",type:"eq"},
{key:"active",value:true,type:"eq"}
]
Now I want to query the mongo collection tasks using the given filterCriteria.
tried something using LINQ statements but no use --hardcoding works but dynamically not working.
How can I achieve this???
maybe you are looking for Builders:
public enum FilterType{
eq=1,//equal
gt=2//greater than
}
//************
var builder = Builders<FilterCritertiaModel>.Filter;
var query = builder.Empty;
foreach(var filterCriteriaItem in filterCriteria){
switch (filterCriteriaItem.type) {
case eq:
query &= builder.Eq(filterCriteriaItem.Key, filterCriteriaItem.Value);
case gt:
query &=builder.Gt(filterCriteriaItem.Key, filterCriteriaItem.Value);
//all cases....
}

Find MongoDB document and only matching array elements w/ C# driver

I'm trying to return a document, and that document should have it's array filtered such that it only contains one item. I've seen many similar questions, but none of them deal with dynamic queries. There may be several constraints so I have to be able to keep adding to the filter.
{
"_id" : ObjectId("6058f722e9e41a3d243258dc"),
"fooName" : "foo1",
"fooCode" : 1,
"bar" : [
{
"barCode" : "123",
"barName" : "Rick's Cafe",
"baz" : [
{
"bazId" : "00",
"bazDescription" : "Ilsa"
},
{
"bazId" : "21",
"bazDescription" : "Victor"
}
]
},
{
"barCode" : "456",
"barName" : "Draco Tavern",
"baz" : [
{
"bazId" : "00",
"bazDescription" : "Rick Shumann"
}
]
}
]
}
This is my attempt, it returns a document who's array contains the barCode, and the array's entire contents are included.
Expression<Func<Foo, bool>> filter = x => x.FooCode == 1;
string barCode = "456"
if (!String.IsNullOrEmpty(barCode))
{
Expression<Func<Foo, bool>> newPred =
x => x.Bar.Any(s => s.BarCode == barCode);
filter = filter.CombineAnd(newPred);
}
var fooQuery =
_fooCollection
.Find(filter);
How do I remove non-matching array elements, but only if an array element was specified?
Unwind to convert the single document into a document per nested-array element in the shape of:
{
"_id" : ObjectId("6058f722e9e41a3d243258dc"),
"fooName" : "foo1",
"fooCode" : 1,
"bar": {
"barCode" : "123",
"barName" : "Rick's Cafe",
...
}
}
Match to find the element you want
Group to recombine the nested-array
So the resulting C# might look like:
var fooQuery = _fooCollection.Aggregate()
.Unwind("bar")
.Match(BsonDocument.Parse("{ 'bar.barcode': '"+ barCode + "'}"))
.Group(BsonDocument.Parse("{​​​​​ '_id':'$fooCode' }​​​​​"))
You need to use aggregate in MongoDB.
You can split the array elements with unwind, filter with match, select the keys that you want with project and group with common column like id or something.
MongoDB Aggregation docs: https://docs.mongodb.com/manual/aggregation/

Problem with removing subdocument mongodb c#

I'm trying to remove subdocument called "Data", but my code didn't work
var update = Update.Pull("Meta.$.Data", new BsonDocument(){
{ } // I dont know what I should write in BsonDocument
});
Here's an example of document
"_id" : 2,
"RefId" : null,
"RefIdStr" : "32",
"Meta" : {
"DatabaseRouting" : "{replicaSetName:company}",
"Data" : "{id:1,name:Centrum ,phoneNumber:,nip:76"
}
I solved problem $unset help me:
Database.GetCollection(Users).Update(query, Update.Unset("Meta.Data"));

Query/Find to Return JSON Object in a BsonDocument

I have a document like this:
{ "File" : "xxxxxxx.txt",
"Content" : [
{ "tag" : "Book",
"name" : "TestBook1",
"value" : "xxx"
},
{ "tag" : "Dept",
"name" : "TestDept1",
"value" : "yyy"
},
{ "tag" : "Employee",
"name" : "TestEmployee1",
"value" : "zzz"
}]
}
I can find the document by using:
var filter = Builders<BsonDocument>.Filter.Eq("Content.tag", "Dept");
var result = collection.Find(filter).ToList();
However, this returns the whole document. Is there any way to just get the JSON object ({"tag" : "Dept", "name" : "TestDept1"})?
What I'm trying to get is just the "value" property (in this case, it's "yyy"), instead of the whole document.
UPDATE:
Like Phani suggested, I was able to make this work with the following code:
var subFilter = Builders<BsonDocument>.Filter.Eq("tag", "Dept");
var filter = Builders<BsonDocument>.Filter.ElemMatch("Content", subFilter);
var result =
collection.Find(filter)
.Project(Builders<BsonDocument>.Projection.Exclude("_id").Include("Content.$"))
.ToList();
You need to use the ElemMatch projection for this.
Shell query for this : db.testing.find({Content:{$elemMatch:{"tag":"Dept"}}},{"_id":0,"Content.$":1})
C# query will be
Find(x => x.Content.Any(p => p.tag == "Dept")).Project(Builders<BsonDocument>.Projection.Exclude("_id").Include("Content.$")).ToList();
Please check if this is working.

C# MongoDB index causes weird duplicate exceptions

we have a problem with our indexes. We have an index on our emails but it throws errors like such:
> db.User.insert({email: "hell33o#gmail.com", "_id" : BinData(3,"iKyq6FvBCdd54TdxxX0JhA==")})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error index: placetobe.User.$email_text dup key: { : \"com\", : 0.6666666666666666 }"
}
})
when we have the index created with our C# driver like this
Created by C# with:
CreateIndexOptions options = new CreateIndexOptions {Unique = true};
_collection.Indexes.CreateOneAsync(Builders<User>.IndexKeys.Text(_ => _.email), options);
resulted in
{
"v" : 1,
"unique" : true,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "email_text",
"ns" : "placetobe.User",
"weights" : {
"email" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 2
}
but if we create it with the MongoDB console like this it works:
{
"v" : 1,
"unique" : true,
"key" : {
"email" : 1
},
"name" : "email_1",
"ns" : "placetobe.User"
}
I don't understand the difference between the two indexes, but they have an effect on our DB. We also have problems with a Collectin that saves names. we get duplicate exceptions on "Molly" if we try to insert "Molli". With the emails is seems to give us errors whenever we have two "gmail" emails in the collection or two ".com" emails etc.
This is a University project and we have to turn it in tomorrow. we're really in trouble, help would be much appreciated
You don't want your email to be a text Index. Text indices allow you to search large amounts of text in MongoDB like if you were parsing through comments or something. All you want is to make sure your emails aren't duplicated so you should use an ascending or descending index.
CreateIndexOptions options = new CreateIndexOptions {Unique = true};
_collection.Indexes.CreateOneAsync(Builders<User>.IndexKeys.Ascending(_ => _.email), options)

Categories