私はMongoDBを学んでいます。私はC#を使って試してみたいと思います。 C#公式MongoDBドライバを使用して強く型付けされたMongoDBドキュメントで操作することは可能ですか?mongoDB、C#の公式ドライバを使った強く型付けされたコレクション
私はクラスAlbum
とPhoto
を持っている:
public class Album : IEnumerable<Photo>
{
[Required]
[BsonElement("name")]
public string Name { get; set; }
[Required]
[BsonElement("description")]
public string Description { get; set; }
[BsonElement("owner")]
public string Owner { get; set; }
[BsonElement("title_photo")]
public Photo TitlePhoto { get; set; }
[BsonElement("pictures")]
public List<Photo> Pictures { get; set; }
//rest of the class code
}
public class Photo : IEquatable<Photo>
{
[BsonElement("name")]
public string Name;
[BsonElement("description")]
public string Description;
[BsonElement("path")]
public string ServerPath;
//rest of the class code
}
私はtest
データベースに収集albums
に新しい文書を挿入します。私はBsonDocument
で操作したくないですが、強くタイプされたAlbum
を使用することをお勧めします。私はそれはのようなものだろうと思った:
IMongoClient client = new MongoClient();
IMongoDatabase db = client.GetDatabase("test");
IMongoCollection<Album> collection = database.GetCollection<Album>("album");
var document = new Album
{
Name = album.Name,
Owner = HttpContext.Current.User.Identity.Name,
Description = album.Description,
TitlePhoto = album.TitlePhoto,
Pictures = album.Pictures
};
collection.InsertOne(document);
しかし、それは私に次のエラー与える:私が間違っているのは何
An exception of type 'MongoDB.Driver.MongoCommandException' occurred in MongoDB.Driver.Core.dll but was not handled in user code
Additional information: Command insert failed: error parsing element 0 of field documents :: caused by :: wrong type for '0' field, expected object, found 0: [].
をし、それを達成することが可能かどう?
[それは助けかもしれない](http://www.codeproject.com/Articles/273145/Using-MongoDB-with-the-Official-Csharp-Driver) – Chandralal