2

I have MongoDB collection

/* 0 */
{
    "_id" : ObjectId("55eafc733d845f2b0cf05944"),
    "TypeOfStudies" : "ZAO",
    "PublishDateUtc" : ISODate("2015-08-26T22:00:00.000Z"),
}

/* 1 */
{
    "_id" : ObjectId("5601977f3d845f09c0051225"),
    "TypeOfStudies" : "STAC",
    "PublishDateUtc" : ISODate("2015-09-21T22:00:00.000Z"),
}

/* 2 */
{
    "_id" : ObjectId("5601977f3d845f09c0051226"),
    "TypeOfStudies" : "ZAO",
    "PublishDateUtc" : ISODate("2015-09-21T22:00:00.000Z"),
}

For querying my storage I use Simple.Data.MongoDB adapter.

This piece of code should return the latest and oldest item with TypeOfStudies set to "ZAO".

var orderedDesc = _db.Timetables
    .FindAllByTypeOfStudies(typeOfStudies.GetAbbrv())
    .OrderByPublishDateUtcDescending();

var orderedAsc = _db.Timetables
    .FindAllByTypeOfStudies(typeOfStudies.GetAbbrv())
    .OrderByPublishDateUtc();


var firstFromAsc = orderedAsc.FirstOrDefault();
var firstFromDesc = orderedDesc.FirstOrDefault();
// and what we get is: firstFromAsc.PublishDateUtc == firstFromDesc.PublishDateUtc (?!)

Unfortunetly, no matter what I do, firstFromAsc and firstFromDesc remain the same.

I've tried also with forcing adapter to retrieve only the first element, but nothing has changed.

_db.Timetables
            .FindAllByTypeOfStudies(typeOfStudies.GetAbbrv())
            .OrderByPublishDateUtcDescending()
            .FirstOrDefault()

To see if it's something wrong with my code or adapter itself, I've created a small test program which proves that adapter works correctly.

class Program
{
    private static dynamic _db;

    static void Main(string[] args)
    {
        Bootstrap();

        GetAllRows();

        GetLatest();

        Console.WriteLine("END");
        Console.ReadKey();
    }

    private static void GetLatest()
    {
        Console.WriteLine("Latest");
        var latest = _db.Records.FindAllByType(1)
                                .OrderByCreateDateDescending()
                                .FirstOrDefault();
        Console.WriteLine($"Name: {latest.Name}, CreateDate: {latest.CreateDate}, Type: {latest.Type}");
    }

    private static void GetAllRows()
    {
        Console.WriteLine("All rows:");
        var rows = _db.Records.All().OrderByCreateDateDescending();
        foreach (var row in rows)
        {
            Console.WriteLine($"Name: {row.Name}, CreateDate: {row.CreateDate}, Type: {row.Type}");
        }
    }

    static void Bootstrap()
    {
        _db = GetDb();
        LoadTestDataToDb();
    }

    private static void LoadTestDataToDb()
    {
        _db.Records.Insert(Name: "Alice", CreateDate: new DateTime(2014, 7, 13), Type: 1);
        _db.Records.Insert(Name: "Betty", CreateDate: new DateTime(2015, 6, 15), Type: 1);
        _db.Records.Insert(Name: "Cindy", CreateDate: new DateTime(2015, 7, 22), Type: 1);
        _db.Records.Insert(Name: "Diana", CreateDate: new DateTime(2015, 7, 22), Type: 2);
    }

    private static dynamic GetDb()
    {
        return Database.Opener.OpenMongo("mongodb://localhost:27017/SimpleMongoFirst");
    }
}

After running I get this

All rows:
Name: Diana, CreateDate: 21.07.2015 22:00:00, Type: 2
Name: Cindy, CreateDate: 21.07.2015 22:00:00, Type: 1
Name: Betty, CreateDate: 14.06.2015 22:00:00, Type: 1
Name: Alice, CreateDate: 12.07.2014 22:00:00, Type: 1
Latest
Name: Cindy, CreateDate: 21.07.2015 22:00:00, Type: 1
END

Any suggestions?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
pizycki
  • 1,249
  • 4
  • 14
  • 26
  • `FindAllByTypeOfStudies` and `OrderByPublishDateUtcDescending` seem to be your custom methods. You should include their implementation code in your question. – Dmytro Shevchenko Sep 23 '15 at 15:44
  • They're not my methods, it just the way Simple.Data builds queries :) See more about Simple.Data here http://simplefx.org/simpledata/docs/index.html – pizycki Sep 23 '15 at 15:46

0 Answers0