简体   繁体   English

我如何将这个mongodb查询转换为C#驱动程序?

[英]How can i translate this mongodb query to c# driver?

How can i write a compatible c# code for this? 我如何为此编写兼容的C#代码?

I know i can do projection like this: 我知道我可以这样投影:

var projection = Builders<BsonDocument>.Projection.Include("title");

But no idea how to project the last name to get the author's last name after a lookup aggregation 但是不知道如何在查找聚合后投影姓氏以获得作者的姓氏

db.books.aggregate(
   [
      {
         $project: {
            title: 1,
            lastName: "$author.lastName",
         }
      }
   ]
)

Try this one 试试这个

            var project = new BsonDocument
            {
                {
                    "$project",
                    new BsonDocument
                    {
                        {"title", 1},
                        {"lastName", "$author.lastName"},
                    }
                }
            };

            var pipelineLast = new[] { project };


            var resultLast = db.books.Aggregate<BsonDocument>(pipelineLast);
            var matchingExamples = await resultLast.ToListAsync();
            foreach (var example in matchingExamples)
            {
// Display the result 
            }

assuming your author entity is embedded inside the book entity, here's a strongly typed solution. 假设您的作者实体嵌入在书本实体内部,那么这是一个强类型化的解决方案。 if your author is a referenced entity, let me know and i'll update my answer. 如果您的作者是被引用的实体,请告诉我,我将更新答案。

using MongoDB.Entities;
using System;
using System.Linq;

namespace StackOverflow
{
    public class Program
    {
        public class Book : Entity
        {
            public string Title { get; set; }
            public Author Author { get; set; }
        }

        public class Author
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }

        private static void Main(string[] args)
        {
            new DB("test");

            (new Book
            {
                Title = "a book title goes here",
                Author = new Author
                {
                    FirstName = "First Name",
                    LastName = "Last Name"
                }
            }).Save();

            var res = DB.Queryable<Book>()
                        .Select(b => new
                        {
                            Title = b.Title,
                            LastName = b.Author.LastName
                        }).ToArray();

            foreach (var b in res)
            {
                Console.WriteLine($"title: {b.Title} / lastname: {b.LastName}");
            }

            Console.Read();
        }
    }
}

the above code is using my library MongoDB.Entities for brevity. 上面的代码使用我的MongoDB.Entities库来简化。 simply replace DB.Queryable<Book>() with collection.AsQueryable() for the official driver. 只需将DB.Queryable<Book>()替换为官方驱动程序的collection.AsQueryable()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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