Giter Club home page Giter Club logo

Comments (7)

kofrasa avatar kofrasa commented on July 18, 2024 1

Taking another look at this issue, it hit me that a collection abstraction is not necessary at all if we are willing to break a bit of MongoDB convention. The from field can refer directly to the join collection.

Example:

{
   $lookup:
     {
       from: <collection to join>, // this will be an Array
       localField: <field from the input documents>,
       foreignField: <field from the documents of the "from" collection>,
       as: <output_field> 
     }
}

In fact this is similar to what @justsml suggested above but will put the implementation of data sourcing outside of Mingo.

If a Function value is used for the from field, it will be called when the pipeline stage is reached to obtain the data to use. This is nice for a lazy effect and allows us to pass in values should we choose, which then can impact what the function will yield for the join. The latter part (params to function) is yet to be considered thoroughly.

Any comments?

from mingo.

kofrasa avatar kofrasa commented on July 18, 2024

I have only looked at the $lookup operator following your question and it seems to reference another collection to function. This is not particularly suitable for Mingo which only works per collection without a full backing database.

Perhaps you can experiment with an implementation using the custom operators feature. Please feel free to send a PR

from mingo.

minicuper avatar minicuper commented on July 18, 2024

Yup, it works with two collections. For the first glance it does not match Mingo but it makes aggregation pipeline much more powerful and expressive. It's the analog of join from the SQL world.

from mingo.

justsml avatar justsml commented on July 18, 2024

This would be cool if data was provided through promises, then you could point to a restful URI wrapper to reference collections:

{$lookup: {
    from: collection('/api/users?active=true'), // <-- promis-ey thing, 
    localField: "email",
    foreignField: "username"
  }
}

I'll play around with the custom operators...

from mingo.

minicuper avatar minicuper commented on July 18, 2024

I thought a lot about this and it seems it would be better if we have 'collection' abstraction in Mingo. It would allow us to implement $lookup as well as $out and so on.

I understand that it is a big shift and leads to a lot of refactoring, but it looks like the only good solution. We might implement this seamless way. Without changing existing API. Just add something like this:

let usersData = [
  // ...
]
let users = Mingo.collection('users', usersData)
users.find( ... )
users.aggregate( ... ) 

// let data = users.toArray() - to get the array back
// or
// let users = Mingo.collection('users') - if it already exists in Mingo 
//                                         and we just want to get a link 

What do you think guys? It isn't a heavy solution, is it?

from mingo.

kofrasa avatar kofrasa commented on July 18, 2024

This did cross my mind a while ago but I was deterred from going with it. My concern is that it makes Mingo begin to look like a mini MongoDB database which I want to avoid as much as possible.

I believe someone mentioned a similar project minimongo that attempts to do and also has that update support.

Once we begin to hold collection references it opens up a can of worms of rules regarding immutability guarantees etc. I feel that is more suitable for an adjacent project which perhaps uses Mingo for the querying.

Have you had any success implementing $lookup as a custom operator?

You may have to modify the original structure a bit to make it work by passing in the extra collection. It is ok to break a few rules to get Mingo to work for its purpose so the result need not be exactly as in MongoDB.

Let's keep the discussion going. I think we will come to an acceptable solution.

Thanks for pushing.

from mingo.

kofrasa avatar kofrasa commented on July 18, 2024

Added in commit 6ae6509
Currently, the from field must be an array.

from mingo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.