0

i have problems when i trying to run raw mongodb queries on Loopback 3, here is my code :

MyModel.getDataSource().connector.connect(function(err, db) {
    var collection = db.collection('my_model');
    collection.aggregate([
        {$group : { _id: "$logid", "row":{"$first":"$$ROOT"}, count: { $sum: 1 } } },
        {$match: {_id :{ $ne : null } , count : {$gt: 1} } }, 
        {$sort: {count : -1} },
        {$project: {logid : "$_id", "_id" : 0, code:"$row.Code", id:"$row._id", model_type:"$row.model_type"} }     
      ], function(err, data) {
      if (err) return callback(err);
      return callback(null, data);
    });
  });

but when i running the program it return error on cmd like this :

 Cannot read property 'connector' of null

this is my datasource

"mongodb": {
"host": "127.0.0.1",
"port": 27017,
"database": "MyDatabase",
"name": "mongodb",
"connector": "mongodb",
"allowExtendedOperators": true }

thankyou in advance

azukhrufy
  • 1
  • 1
  • something related. https://stackoverflow.com/questions/34109804/loopback-cannot-read-property-connector-of-null-in-connector-hooks check upvoted one. do you need MyModel.getDataSource().mongodb.connector.connect(function(err,db){})? if MyModel.getDataSource() is returning { "mongodb": { "host": "127.0.0.1", "port": 27017, "database": "MyDatabase", "name": "mongodb", "connector": "mongodb", "allowExtendedOperators": true } } – Deeksha Pandit Mar 05 '21 at 07:28
  • i have read that question and the solution, but i have misunderstanding about the answer, because based on answer, we must create one addition file boot with variable name 'mongoConnector' as a connector, so in my project we can connect mongodb like " MyModel.getDataSource().mongoConnector.connect(function(err, db) " ? – azukhrufy Mar 05 '21 at 07:40

0 Answers0