我第一次使用MongoDB.
我看过这个例子:
SELECT a,b FROM users WHERE age = 33 db.users.find({age:33},{a:1,b:1})
但我无法将其翻译成C#.谁能帮我?
我有一个课程User,我需要在Web服务中使用它们.
然后问题是如果我尝试序列化Id那种类型BsonObjectId,我看到它有一个空属性,有一个空属性,依此类推......
我按顺序编写了这个解决方法,这是一个很好的解决方案吗?
public partial class i_User
{
[BsonId(IdGenerator = typeof(BsonObjectIdGenerator))]
[NonSerialized]
public BsonObjectId _id;
public String Id
{
get
{
return this._id.ToString();
}
}
}
Run Code Online (Sandbox Code Playgroud)
通过这种方式,我可以保持_Id,BsonObjectId但我在该属性的Web上发送字符串表示Id.
另一种解决方案是使用 StringObjectIdGenerator
public partial class i_User
{
[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
public String id;
}
Run Code Online (Sandbox Code Playgroud)
但是看到MongoDB将存储string到数据库而不是ObjectId.
在Web服务和/或客户端服务器(Flash + C#)等序列化环境中工作的最佳方法是什么?
我的数据库中有一个"BSonElement",我用标准查询重试它.
问题是我无法将BsonDocument转换为Type.
例:
更新1:
public partial class item_Stat
{
[BsonExtraElements]
public BsonDocument all_stat;
}
Run Code Online (Sandbox Code Playgroud)
基本上,我已经进入我的DB 10-15属性(字段),我可以用"BsonExtraElements"阅读.通过这种方式,我可以重试属性而无需在C#中定义它.
all_stat,可以有10-15-20属性,在dinamically变化.C#是键入的语言,所以我无法在C#中定义此属性,并且我使用了ExtraElements.
问题是当我从DB查询对象时.
var item_db = myMongoCollection.find(theQuery); // find the OBJECT
item_db.all_stat // all the property hare HERE
// find the property "category_01"
var i = item_db.all_stat.Where(p => p.Name == "category_01").Single();
// ok, i have found the Category, so i can cast it to C# Data Type
var typed_value = (ItemStatSingle) i.Value // BsonElement to ItemStatSingle
Run Code Online (Sandbox Code Playgroud)