How to modify a child element in a child array of a document in MongoDB? I'm using the latest C# Driver for MongoDB (C# Driver Version: 2.5, Server Version: 3.2.0) and I've tried solutions from multiple answers on this from SO (duplicate questions also) and from various other sources. Most of the solutions proved to be obsolete.
Below is my sample document:
[{
"_id" : "69c4f77d-05ef-431d-ae17-c076c6173e04",
"title" : "Root Page",
"createdAt" : ISODate("2017-12-21T12:28:00.680+0000"),
"modifiedAt" : ISODate("2017-12-26T07:18:11.165+0000"),
"pages" : [
{
"_id" : "f449dc0b-3d1b-4a59-b622-6a42ce10b147",
"title" : "Test page 1",
"slug" : "test-page-1",
"createdAt" : ISODate("2017-12-21T12:28:00.680+0000"),
"modifiedAt" : ISODate("2017-12-21T12:28:00.680+0000")
},
{
"_id" : "3d1497d7-f74c-4d88-b15c-bf2f9c736374",
"title" : "Test page 2",
"slug" : "test-page-2",
"createdAt" : ISODate("2017-12-25T11:27:55.006+0000"),
"modifiedAt" : ISODate("2017-12-25T11:27:55.006+0000")
},
{
"_id" : "6e827e2a-5a25-4343-b646-885816bb8cc4",
"title" : "Test page 3",
"slug" : "test-page-3",
"createdAt" : ISODate("2017-12-25T11:31:16.516+0000"),
"modifiedAt" : ISODate("2017-12-25T11:31:16.516+0000")
}]
},
...,
...
]
Document Structure: The Root Document contains an array of sub documents, which has it's metadata and a list of pages (array)
Inside a Sub Document - I'm trying to update the title and slug of a child page object inside pages array, but have no luck so far with the latest driver.
I'm using the following filter and update query:
var filter = Builders<SubDocument>.Filter.And(Builders<SubDocument>.Filter.Eq("_id", "xxx"), Builders<SubDocument>.Filter.ElemMatch(x => x.Pages, p => p.Id == "xxx"));
var update = Builders<SubDocument>.Update
.Set("pages.$.title", "changed title")
.Set("pages.$.slug", "changed-title")
.Set("pages.$.modifiedAt", DateTime.UtcNow);
_mongoDb.Documents.UpdateOne(filter, update);
Now - this query returns me an error:
Invalid BSON field name pages.$.title
The references I came along for last few hours are saying to use "$" operator but it throws error in latest driver.
[Edit 1] Here's my models:
public class SubDocument
{
public SubDocument()
{
Id = Guid.NewGuid().ToString();
CreatedAt = DateTime.UtcNow;
ModifiedAt = DateTime.UtcNow;
Pages = new List<Page>();
}
[BsonId]
public string Id { get; set; }
[BsonElement("title")]
public string Title { get; set; }
[BsonElement("createdAt")]
public DateTime CreatedAt { get; set; }
[BsonElement("modifiedAt")]
public DateTime ModifiedAt { get; set; }
[BsonElement("pages")]
public List<Page> Pages { get; set; }
}
public class Page
{
[BsonId]
public string Id { get; set; }
[BsonElement("title")]
public string Title { get; set; }
[BsonElement("slug")]
public string Slug { get; set; }
[BsonElement("createdAt")]
public DateTime CreatedAt { get; set; }
[BsonElement("modifiedAt")]
public DateTime ModifiedAt { get; set; }
}
Anyone? Thanks in advance!