question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I would like to ask how to write a query, can help me?

See original GitHub issue

This is my mongodb query statement:

db.buy_sell_points.aggregate([
    {
        $match: {
            "symbol": { $in: ['002597','600004'] }
        }
    },
    {
        $sort: {
            "modified_on": -1
        }
    },
    {
        $group: {
            _id: {
                "symbol": "$symbol",
                "period": "$period"
            },
            "TopModifiedOnDoc": { $first: "$$ROOT" }
        }
    },
    {
        $replaceRoot: { newRoot: "$TopModifiedOnDoc" }
    }
])

I have defined an entity BuySellPoint, but I don’t know how to write this query using MongoDB.Entities.

Issue Analytics

  • State:closed
  • Created a month ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dj-nitehawkcommented, Aug 16, 2023
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using MongoDB.Entities;

await DB.InitAsync("test");

var seedData = new[]
{
    new BuySellPoint{ ID = "1", Period = "january", Symbol = "001", ModifiedOn = DateTime.UtcNow.AddDays(-2)},
    new BuySellPoint{ ID = "2", Period = "january", Symbol = "001", ModifiedOn = DateTime.UtcNow.AddDays(-1)},

    new BuySellPoint{ID = "3", Period = "february", Symbol = "001", ModifiedOn = DateTime.UtcNow.AddDays(-2)},
    new BuySellPoint{ID = "4", Period = "february", Symbol = "001", ModifiedOn = DateTime.UtcNow.AddDays(-1)}
};

await seedData.SaveAsync();

var symbols = new[] { "001", "002" };

var res = await DB
    .Queryable<BuySellPoint>()
    .Where(p => symbols.Contains(p.Symbol))
    .OrderByDescending(p => p.ModifiedOn)
    .GroupBy(p => new { p.Symbol, p.Period })
    .Select(g => g.First())
    .ToListAsync();

foreach (var r in res)
    Console.WriteLine($"top modified: {r.ID}");

//expected to print : 2 & 4

Console.ReadLine();

[Collection("buy_sell_points")]
public class BuySellPoint : Entity //, IModifiedOn
{
    [Field("symbol")]
    public string Symbol { get; set; }

    [Field("period")]
    public string Period { get; set; }

    [Field("modified_on")]
    public DateTime ModifiedOn { get; set; }
}
0reactions
lamjackcommented, Aug 16, 2023

Perfect, It’s works for me. Thanks very much for your help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Write a Query Letter [+ Free Querying Toolkit]
Learn how to write a query letter that will get an agent to request your manuscript. Includes an exclusive querying kit.
Read more >
Help me write this query in SQL - Database Administrators Meta
I need to write a query that returns the min and max of them combined. ... Expected results -- but don't just tell...
Read more >
Create a simple select query
A query is a handy way to save a selection of records. Basic steps to create a select query. You can create a...
Read more >
How to Write a Query Letter That Gets Manuscript Requests
Your query should show what a good writer you are, rather than you telling or emphasizing what a good writer you are. Thank...
Read more >
How to Write better SQL Queries - YouTube
The proper management of important databases helps to improve data-centric processes and increase the value of data assets across the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found