Coder Social home page Coder Social logo

Comments (4)

makersu avatar makersu commented on June 14, 2024

My solution

var connector = Numessage.getDataSource().connector;
var matchWhere = connector.buildWhere(Numessage.modelName,filter.where);
var numessageCollection = Numessage.getDataSource().connector.collection(Numessage.modelName);
numessageCollection.aggregate([
                {$match: matchWhere },
                {$group: { _id: "$owner", total: { $sum: 1 } } }
          ], function(err, groupByRecords) {
            if(err){
                console.log(err)
                cb(err,groupByRecords)
            }
                console.log(groupByRecords)
                groups.owners = groupByRecords
            });

And I found the field "upload_time" from json is string type not date object type, I have to convert to date object, any suggestion? Thank you.

for(var i=0 ;i<data.where.and.length;i++ ){
  if(data.where.and[i].upload_time.gt){
        data.where.and[i].upload_time.gt=new Date(data.where.and[i].upload_time.gt)
  }     
}     

from loopback-connector-mongodb.

dcajio avatar dcajio commented on June 14, 2024

This has gone unanswered for close to a year now. Has there been any traction on this, another way to handle it, or is it still not possible?

from loopback-connector-mongodb.

lkappeler avatar lkappeler commented on June 14, 2024

I came across this Issue because we have Implemented a custom find method with aggregations for a customer project which had the same Issue with the date conversion.

In general this a problem which should not be handled by a specific connector implementation, because it makes no sense in terms of architecture. ( eg every connector would have to implement it. Which could lead in to some strange behaviours when switching the data source.)

The problematic is already handled in the loopback-datasource-juggler package.
The datasource-juggler implements a Method called normalize which then passes the where part of the given filter to the coerce function. This function coerce values based on the property types.

Normally the JSON (eg. query filter) comes from the request and passes this function before being passed to the database specific connector. If this is not the case eg overriding the find function as mentioned in the introduction, you have to pass your filter manually to the normalize function. The given result will then be converted as defined in the model and can be used for the further processing.

Note: To have the right context, you need to call the normalize function on the model itself. In this example the call would be:

...
numessageCollection.aggregate(Numessage._normalize({...Query where...}), function(err, groupByRecords) {
...
}
...

from loopback-connector-mongodb.

ssh24 avatar ssh24 commented on June 14, 2024

What @lkappeler said is correct. Juggler calls a function when a query is passed for a find operation to normalize the query and get rid of unnecessary properties from the query. You can use find directly with a filter that you would like to use or make sure whatever filter you choose is normalized before calling the buildWhere function itself.

Hopefully this answered the question for this issue. Closing this as a result. Feel free to reopen if needed.

from loopback-connector-mongodb.

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.