How would I use BsonElement syntax to map the array of reviews in the sample_airbnd.listingsAndReviews collection into a class for the document?
This isn’t working
[BsonIgnoreExtraElements]
class ListingAndReview
{
[BsonId]
public long Id { get; set; }
[BsonElement("name")]
public String Name { get; set; }
[BsonElement("room_type")]
public String RoomType { get; set; }
[BsonElement("property_type")]
public String PropertyType { get; set; }
[BsonElement("amenities")]
public String[] Amenities { get; set; }
/*
[BsonElement("host")]
[BsonIgnoreIfNull]
public IEnumerable<Host> Host { get; set; }
*/
}
OK, lots of embarrassment for asking this question now.
Below is a sample Mapping Class for the listingsAndReviews collection:
namespace MongoDbPlay
{
[BsonIgnoreExtraElements]
class Host
{
[BsonElement("host_id")] public long HostId { get; set; }
[BsonElement("host_url")] public string HostUrl { get; set; }
[BsonElement("host_name")] public string HostName { get; set; }
}
[BsonIgnoreExtraElements]
class Review
{
[BsonElement("_id")] public long Id { get; set; }
[BsonElement("date")] public DateTime Date { get; set; }
[BsonElement("comments")] public string Comments { get; set; }
}
[BsonIgnoreExtraElements]
class ListingAndReview
{
[BsonId] public long Id { get; set; }
[BsonElement("name")] public string Name { get; set; } = null!;
[BsonElement("room_type")] public string RoomType { get; set; } = null!;
[BsonElement("property_type")] public string PropertyType { get; set; } = null!;
[BsonElement("amenities")] public List<string> Amenities { get; set; }
[BsonElement("host")] [BsonIgnoreIfNull] public Host Host { get; set; }
[BsonElement("reviews")][BsonIgnoreIfNull] public List<Review> Reviews { get; set; }
}
}
My issue was a error message being thrown when just trying to work with the host object and in newbie fashion had [BsonElement(“host_name”)] public long HostName { get; set; } instead of string.
After fixing that it was all “as expected” to arrive at the above sample