score:2
Profiler becomes your friend when you're not sure about driver syntax. To enable logging for all queries you need to run on your database below statement:
db.setProfilingLevel(2)
Then to check last query executed on the database you need to run:
db.system.profile.find().limit(1).sort( { ts : -1 } ).pretty()
So for the first code snippet you will get:
"pipeline" : [ { "$sort" : { "t" : 1 } },
{ "$limit" : 1 }
]
"pipeline" : [ { "$sort" : { "t" : -1 } },
{ "$limit" : 1 }
]
For the second pair it prints
"pipeline" : [ { "$sort" : { "t" : 1 } },
{ "$limit" : 1 }
]
and throws NotSupportedException for LastOrDefault
on my machine, if it works on your MongoDB driver's version you can check generated MongoDB statement using profiler
For the last one when you hover on c
in your Visual Studio it prints
{aggregate([{ "$sort" : { "t" : 1 } }])}
but since it's of type IOrderedQueryable<T>
it is only not materialized query so it will get executed on the database when you run FirstOrDefault
generating the same aggregation body as previous statements. I'm getting NotSupportedException here as well. Here you can find a list of supported LINQ operators and both Last
and LastOrDefault
are not implemented so you need to sort descending.
Source: stackoverflow.com
Related Articles
- in mongodb, what is the most efficient way to get the first and last document
- most efficient Entity Framework Code First method of flattening / projecting parent entity with specific child
- Entity Framework Code Most First Efficient Linq Query
- What is the most efficient way to obtain an ordered range using EntityFramework?
- Most efficient way of getting the N last element of an array
- What is the most efficient way to join two lists in C# on a common field?
- What is the most efficient way to select an XML value based on one of its associative attributes using C#
- What is the difference between First and FirstOrDefault , Last and LastOrDefault
- Most efficient structure for quick access to a random element + the last element on one criteria
- linq-to-sql is this the most efficient code review
- What is the most efficient way or best practice for null check when you use Split and FirstOrDefault methods together?
- What is the most efficient Linq statement to query these tables?
- What is the most efficient way to extract information from a text file formatted like this
- Where, Select and Single in Linq: What is the most efficient use with ASP.NET Core?
- What is the most efficient way to get comparison statistics for data from 2 large tables
- Why is .Contains slow? Most efficient way to get multiple entities by primary key?
- Entity Framework, Code First and Full Text Search
- What does this C# code with an "arrow" mean and how is it called?
- Most efficient algorithm for merging sorted IEnumerable<T>
- What is more efficient for parsing Xml, XPath with XmlDocuments, XSLT or Linq?
- Most efficient way to update with LINQ to SQL
- Entity Framework 6 Code First Custom Functions
- Take the first five elements and the last five elements from an array by one query using LINQ
- Most Efficient way to test large number of strings against a large List<string>
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- passing dynamic expression to order by in code first EF repository
- Entity Framework dbset most efficient way of deleting
- What's the most efficient way to get only the final row of a SQL table using EF4?
- NUnit: What is the most concise way to assert whether an IEnumerable contains an object of a certain type?
- What is the best way to check and retrieve the first item of a collection?
- what is inner equijoin?
- Linq and EF: Querying to get results on one to many relationships: Selecting table with foreign Key
- Linq select returning string instead of object
- The entity or complex type 'CreditFacility_Web.Models.CreditFacilityModel.SavingsAccount' cannot be constructed in a LINQ to Entities query
- Filtering with Let
- How to write string.Contains(someText) in expression Tree
- Model Design As Database Table Or Multi-Dimensional List?
- How to use Distinct option to filter the value of a Column Name that is related to different foreign Keys. C# .NET Lambda LINQ
- Error during execution of query expression in LINQpad
- declaring empty byte - Nullable object must have a value?
- Merging XML Elements with LINQ
- How to use split in linq query?
- How to do a left outer join in Entity Framework without using the query syntax?
- build linq query based off parameters
- Linq Method syntax for retrieving single nested child entity
- Lambda Expression with self reference - what is it doing?
- Dynamic Linq Expression in .NET Core
- linq query using joins and aggregate functions
- Exception in a CRM LINQ query with joins. Attribute in second table doesn't exist
- Persisting Lazy Loaded Properties