1

I have simple class which represents fields in MongoDB document

class Measurement
{
    public ObjectId id { get; set; }
    public int s { get; set; }
    public int[] p { get; set; }
    public int dt { get; set; }
    public int ml { get; set; }
}

I'm trying to get documents matching my conditions using

var collection = database.GetCollection<Measurement>(mongoCollectionName);
    var query = from a in collection.AsQueryable<Measurement>()
                where a.dt > 100
                select a;

When where condition is removed i do receive all documents but with condition none. Response says there's no matching documents but there are (example dt=1538555)

query looks like this {aggregate([{ "$match" : { "dt" : { "$gt" : 100 } } }])}. I build my example using response from this thread and mongodb documentation MongoDB C# Aggregation with LINQ

I would be grateful with solving probably my stupid mistake

1 Answer 1

0

i don't use the c# driver directly anymore so my solution is using MongoDAL.

using System;
using System.Linq;
using MongoDAL;

namespace Example
{
    class Measurement : Entity
    {
        public int s { get; set; }
        public int[] p { get; set; }
        public int dt { get; set; }
        public int ml { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            new DB("measurements");

            var measurement1 = new Measurement
            {
                s = 10,
                ml = 20,
                dt = 100,
                p = new int[] { 1, 2, 3, 4, 5 }
            };

            var measurement2 = new Measurement
            {
                s = 11,
                ml = 22,
                dt = 200,
                p = new int[] { 1, 2, 3, 4, 5 }
            };

            measurement1.Save();
            measurement2.Save();

            var result = (from m in DB.Collection<Measurement>()
                          where m.dt > 100
                          select m).ToArray();

            Console.ReadKey();

        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.