Reputation: 2089
Is there anyway in mongoDB using .Net to create some sort of equivalent to the "SQL-Join"? I have read the MongoDB docs (https://docs.mongodb.com/manual/tutorial/model-referenced-one-to-many-relationships-between-documents/) regarding relationships.. and from what I understand you simply add relationships by refering to their IDs. However.. does this also mean that for each relationship you also need to do one additional query?..
Upvotes: 5
Views: 6834
Reputation: 5669
If you don't mind using a library, MongoDB.Entities can do relationships quite easily without having to do joins manually, unless you really want to :-)
Have a look at the code below, which demonstrates a one-to-many relationship between an author and book entities. the book entity knows nothing of the authors. but you can still get reverse relationship access by supplying either a book ID, an array of book IDs or even an IQueryable of books. [disclaimer: I'm the author of the library]
using System;
using System.Linq;
using MongoDB.Entities;
using MongoDB.Driver.Linq;
namespace StackOverflow
{
public class Program
{
public class Author : Entity
{
public string Name { get; set; }
public Many<Book> Books { get; set; }
public Author() => this.InitOneToMany(() => Books);
}
public class Book : Entity
{
public string Title { get; set; }
}
static void Main(string[] args)
{
new DB("test");
var book = new Book { Title = "The Power Of Now" };
book.Save();
var author = new Author { Name = "Eckhart Tolle" };
author.Save();
author.Books.Add(book);
// Build a query for finding all books that have Power in the title.
var bookQuery = DB.Queryable<Book>()
.Where(b => b.Title.Contains("Power"));
// Find all the authors of books that have a title with Power in them.
var authors = author.Books
.ParentsQueryable<Author>(bookQuery);
// Get the result
var result = authors.ToArray();
// Output the aggregation pipeline
Console.WriteLine(authors.ToString());
Console.ReadKey();
}
}
}
Upvotes: 2
Reputation: 49945
Considering simplest relationship like below:
db.publishers.save({
_id: "oreilly",
name: "O'Reilly Media",
founded: 1980,
location: "CA"
})
db.books.save({
_id: 123456789,
title: "MongoDB: The Definitive Guide",
author: [ "Kristina Chodorow", "Mike Dirolf" ],
published_date: ISODate("2010-09-24"),
pages: 216,
language: "English",
publisher_id: "oreilly"
})
In MongoDB you can use $lookup operator to get the data from both collections in one query:
db.books.aggregate([
{
$lookup: {
from: "publishers",
localField: "publisher_id",
foreignField: "_id",
as: "publisher"
}
}
])
which returns:
{
"_id" : 123456789,
"title" : "MongoDB: The Definitive Guide",
"author" : [ "Kristina Chodorow", "Mike Dirolf" ],
"published_date" : ISODate("2010-09-24T00:00:00Z"),
"pages" : 216,
"language" : "English",
"publisher_id" : "oreilly",
"publisher" : [ { "_id" : "oreilly", "name" : "O'Reilly Media", "founded" : 1980, "location" : "CA" } ]
}
Using MongoDB .NET Driver you can use LINQ syntax and join
operator which will get translated into $lookup
:
var books = db.GetCollection<Book>("books");
var publishers = db.GetCollection<Publisher>("publishers");
var q = from book in books.AsQueryable()
join publisher in publishers.AsQueryable() on
book.publisher_id equals publisher._id
select new
{
book,
publisher = publisher
};
var result = q.ToList();
which is translated into $lookup
with $unwind so that you get a single publisher
object instead of array
Upvotes: 6