我有一个具有任意数量属性的ExpandoObject.我想将这些属性作为BsonDocument保存到MongoDB数据库.我尝试使用以下代码执行此操作:
private BsonDocument GetPlayerDocument(IPlayer player)
{
var ret = new BsonDocument();
ret.Add("FirstName", player.FirstName).
Add("LastName", player.LastName).
Add("Team", player.Team).
Add("Positions", new BsonArray(player.Positions));
foreach (var stat in (IDictionary<String, Object>)player.Stats)
{
ret.Add(stat.Key, stat.Value.ToBson());
}
return ret;
}
Run Code Online (Sandbox Code Playgroud)
但是,在对象上调用扩展方法ToBson()时,我收到以下异常:当State为:Initial时,不能调用WriteInt32.
我所知道的唯一的WrtieInt32是Marshall类的静态方法.我接近这个错吗?
更特别的是,有一堂课
class X {
....
string Id { get; set; }
}
class Y : X {
ObjectId MyId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我希望MyId成为Y的id,即映射到_id.
可能吗?
我在这段代码后得到一个例外:
var ys = database.GetCollection("ys");
ys.InsertBatch(new Y[] { new Y(), new Y()});
Run Code Online (Sandbox Code Playgroud)
例外情况:{"类'MongoTest1.Y'的成员'MyId'不能使用元素名称'_id',因为它已被成员'Id'使用."}
完整的测试案例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
namespace MongoTest1
{
class X
{
public string Id { get; set; }
}
class Y : X
{
public ObjectId MyId { get; set; } …Run Code Online (Sandbox Code Playgroud) 我刚才读到某个地方,将来每个文档可以使用32 MB吗?有谁知道这会发生什么时候?或者可能已经存在?
谢谢!
我正在尝试生成一个查询,查找成本大于3的所有大型红色东西.
这个查询似乎就是我所追求的:
{ "color" : "red", "size" : "large", "cost" : { "$gt" : 3.0 } }
Run Code Online (Sandbox Code Playgroud)
但是,我无法找到使用官方MongoDB CSharp驱动程序创建成本条件的优雅方法.这是一个似乎创建查询的hack:
QueryConditionList gt = Query.GT("cost", BsonDouble.Create(3));
QueryDocument query = new QueryDocument();
query.Add("color", "red");
query.Add("size", "large");
query.Add(gt.ToBsonDocument().Elements);
List<BsonDocument> results = events.Find(query).ToList();
Run Code Online (Sandbox Code Playgroud)
另一种似乎有效的方法是这样的:
QueryDocument query = new QueryDocument();
query.Add("color", "red");
query.Add("size", "large");
query.Add("cost", new BsonDocument("$gt", BsonDouble.Create(3)));
List<BsonDocument> results = events.Find(query).ToList();
Run Code Online (Sandbox Code Playgroud)
这些方法中的任何一种都是实现这一目标的好方法吗?还有另一个吗?
我需要使用允许我动态构建查询并添加将在查询中涉及的字段的技术.我希望找到一种通过query.Add()添加条件的方法,但我不知道这是否可行.
任何帮助表示赞赏.
使用MongoDB和最新的10gen C#驱动程序(CSharpDriver-1.3.1.4349),我正在尝试进行"就地"更新并获取结果中影响的文档数量.
public static long SaveListings(string state, bool isActive, DateTime updateDate)
{
var result = Collection().Update(
Query.And(
Query.EQ("State", state),
Query.And(
Query.EQ("IsActive", isActive),
Query.LT("UpdateDate", updateDate))),
Update.Set("IsActive", false), UpdateFlags.Multi);
return result != null ? result.DocumentsAffected : -1;
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,结果为null.如果我是从控制台执行此操作,我可以通过执行以下操作来获取行数:
db.Listing.update( { State: state.Abbreviation, IsActive: true, UpdateDate: { $lt: expiredDate } }, { $set: { IsActive: false } }, false, true);
var numRows = db.getLastErrorObj().n;
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么,或者这是C#驱动程序中的错误?
我正在appharbor.com上运行一个应用程序,我正在使用MongoHQ的数据库.
不时形成我看到这个错误"服务器实例penny.mongohq.com:10070已不再连接." 在appharbor上.
我还看到了一些System.Net.Sockets.SocketException,"连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机无法响应"
有没有办法确保如果服务器没有连接,那么尝试重新连接?或者设定更长的时间?
使用BsonClassMap,是否可以映射域对象引用,同时保持域对象程序集持久无知(将public A Reference { get; set; }属性更改为下面public MongoDBRef Reference{ get; set; }的示例类B是不可接受的).
对于这种情况,引用的对象不是同一聚合的一部分,不应存储为嵌套文档.
是否可以在这样的关系中映射两个域对象:
public class A
{
public Guid Id {get; private set; }
}
public class B
{
public Guid Id { get; private set; }
public A Reference { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
进入以下文档结构:
// Collection for class A
{ _id: "11111111-1111-1111-1111-111111111111" }
// Collection class B
{
_id: "22222222-2222-2222-2222-222222222222",
reference_id: "11111111-1111-1111-1111-111111111111"
}
Run Code Online (Sandbox Code Playgroud)
映射可能如下所示:
BsonClassMap.RegisterClassMap<A>(cm =>
{
cm.MapIdProperty(c => c.Id)
.SetIdGenerator(new …Run Code Online (Sandbox Code Playgroud) 我使用此行来设置日期时间默认值。
DateTimeSerializationOptions.Defaults = DateTimeSerializationOptions.LocalInstance;
Run Code Online (Sandbox Code Playgroud)
我得到这个警告。 'MongoDB.Bson.Serialization.Options.DateTimeSerializationOptions.Defaults'已过时:'创建并注册带有所需选项的DateTimeSerializer。'
但是我找不到改变它的示例...我该如何改变这种绝对用法?
我有一个副本集,我想让rs.status()来分析它.我如何运行rs.status()来自C#驱动程序的命令?
使用旧的驱动程序,我可以指定要从查询返回的字段,如下所示:
var cursor = Collection.Find(query).
SetFields(Fields<MealPlan>.Exclude (plan => plan.Meals));
Run Code Online (Sandbox Code Playgroud)
如何使用2.0驱动程序完成此操作?