简体   繁体   中英

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

I am new to the MongoVerse, but my problem is as follows:

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

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. 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<>).

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.

I'm curious about the Linq issue? 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. (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.

I am using MongoDB 2.6 Standard for Windows 8 (64 bit windows and mongo). I've also reproduced the bug on a Windows 7 machine as well.

I never found the answer, but I changed my approach. (10/9/2014)

I did the serialization myself with the BsonDocument classes. Write times went from 60ms to less than 1ms. Replacing one item in list before took about 6 ms. I still am not sure how to replace an arbitrary BsonArray element, but its fast enough that it is no longer an issue.

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. However, this looks like a bug we should fix. Feel free to file a Jira ticket at jira.mongodb.org under the CSHARP project.

For now, you'll need to resort to using the untyped query builder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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