简体   繁体   English

MongoDB,C#,Linq,查询,通用列表项上的更新

[英]MongoDB, C#, Linq, Query, Update on a generic List item

I am new to the MongoVerse, but my problem is as follows: 我是MongoVerse的新手,但我的问题如下:

var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase("test");
var collection = database.GetCollection<Entity>("entities");

var entity = new Entity { Name = "Tom" };
collection.Insert(entity);
var id = entity.guid;

var query = Query<Entity>.EQ(e => e.guid, id);

var entity2 = collection.FindOne(query);
Console.WriteLine("Getting entity2 works!");

for (int i = 0; i < 100; i++)
{
     var ourPoint = ((MongoQueryable<List<SubEntity<object>>>)(from q in collection.AsQueryable<Entity>() where q.guid == id select q.thePackage)).GetMongoQuery();
     var ourUpdate = Update<List<SubEntity<object>>>.Set(q => q[i], new SubEntity<object>() { value = 3.14 });
     collection.Update(ourPoint, ourUpdate);
}

var entity3 = collection.FindOne(query);
Console.WriteLine("Getting entity3 throws an error!");

System.IO.FileFormatException: Element '0' does not match any field or property of class namespace+Entity System.IO.FileFormatException:元素“ 0”与类名称空间+ Entity的任何字段或属性都不匹配

The class I'm serializing is as follows: 我正在序列化的类如下:

public class Entity
{
    [BsonId]
    public ObjectId guid = new ObjectId();
    public string Name = "";
    public List<SubEntity<object>> thePackage = new List<SubEntity<object>>();

    public Entity()
    {
        for (int i = 0 ; i < 100; i++)
        {
            thePackage.Add(new SubEntity<object>() { value = Math.PI });
        }
    }
}

The subclass is: 子类是:

public class SubEntity<T>
{
    public DateTime dateTime = DateTime.Now;
    public T value = default(T);
}

I suspect, the Linq query isn't supported, or isn't occurring properly. 我怀疑Linq查询不受支持,或者没有正确进行。 What I see from the documentation, is that the Select query is preformed client side, but using StopWatches, the time needed to perform the Operation appear right (vs. over-writing an entire List<>). 我从文档中看到的是Select查询是在客户端执行的,但是使用StopWatches,执行操作所需的时间似乎正确了(与覆盖整个List <>相比)。

I read some stuff on Data Analysis Schema (on Mongo's site), the popular scheme, seems to involve creating a document with an array full of blank entries (for the hour, day, week, depending on how often you're taking a number), and then writing one value at time. 我在流行的方案Data Analysis Schema(在Mongo的站点上)上读了一些东西,似乎涉及到创建一个文档,该文档的数组中包含空白条目(小时,天,周,具体取决于您使用数字的频率) ),然后一次写入一个值。

I'm curious about the Linq issue? 我对Linq问题感到好奇吗? Anyone know what my next stop would be? 有人知道我的下一站将是什么吗? But, I really just want to get a good example of how to update 1 value at a time from an array. 但是,我真的只是想获得一个很好的示例,说明如何从数组中一次更新1个值。 (Where I may not know the data type, or the array size ahead of time) (在我可能不知道数据类型或数组大小的地方)

I will keep looking (its quite late atm, so my search may be postponed), and I will post back my answer if I discover one. 我将继续寻找(它的atm太晚了,所以我的搜索可能会推迟),如果找到答案,我会回发我的答案。

I am using MongoDB 2.6 Standard for Windows 8 (64 bit windows and mongo). 我正在使用Windows 8的MongoDB 2.6标准版(64位Windows和mongo)。 I've also reproduced the bug on a Windows 7 machine as well. 我还复制了Windows 7计算机上的错误。

I never found the answer, but I changed my approach. 我没有找到答案,但是我改变了方法。 (10/9/2014) (10/9/2014)

I did the serialization myself with the BsonDocument classes. 我自己使用BsonDocument类进行了序列化。 Write times went from 60ms to less than 1ms. 写时间从60ms减少到不到1ms。 Replacing one item in list before took about 6 ms. 替换列表中的一项大约需要6毫秒。 I still am not sure how to replace an arbitrary BsonArray element, but its fast enough that it is no longer an issue. 我仍然不确定如何替换任意BsonArray元素,但是它的速度足够快,不再是一个问题。

This line isn't supported: 不支持此行:

Update<List<SubEntity<object>>>.Set(q => q[i], new SubEntity<object>() { value = 3.14 });

We need an element name in the first argument, and q[i] is not one. 我们在第一个参数中需要一个元素名称,而q [i]不是一个。 However, this looks like a bug we should fix. 但是,这看起来像是我们应该修复的错误。 Feel free to file a Jira ticket at jira.mongodb.org under the CSHARP project. 随时在CSHARP项目下的jira.mongodb.org上提交Jira票证。

For now, you'll need to resort to using the untyped query builder. 现在,您需要使用无类型查询构建器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM