loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Question - How to use Mongodb GridFS with loopback?

Open veekungx opened this issue 12 years ago • 14 comments

LB currently provide storage service to store any file.

But if I want to use MongoDB gridfs to store files(pdf,excel,word etc..) and photos instead. There are some question.

1. How to do that? 1.1 Do I have to dedicated to new instance and provide API with mongo native driver then connect with REST connector? 1.2 Can I use "loopback connector" to access Mongo native driver gridFS and aggregate?

something like this

collection.aggregate([
          { $project : {
            author : 1,
            tags : 1
          }},
          { $unwind : "$tags" },
          { $group : {
            _id : {tags : "$tags"},
            authors : { $addToSet : "$author" }
          }}
        ], function(err, result) {
          console.dir(result);
          db.close();
      });

veekungx avatar Sep 21 '14 14:09 veekungx

I found it.

app.dataSources.mongo.connect(function(err,db){
        db.collection('parent').findOne({},function(err,doc){
            console.log(doc)
        })
    })

veekungx avatar Sep 21 '14 16:09 veekungx

:+1:

esco avatar Dec 14 '14 00:12 esco

Hi guys, I've tried such solution, but I get "err" and "db" parameters as undefined. Is it still the right solution for working with native mongodb queries?

Thanks in advance!

thiagobrunoms avatar Aug 16 '15 13:08 thiagobrunoms

Hi, How to get all the collection names in mongo database using Strongloop?

sureshkoduri avatar Jan 29 '16 10:01 sureshkoduri

I'm having same problem here. With mongodb "in" operator when using filter is not working.. i'm trying to execute native query in mongodb and err,db is undefined.

mariohmol avatar Feb 24 '16 13:02 mariohmol

Here is how I did in a MyCollection.js

var ds = MyCollection.app.dataSources.mydb; ds.connector.collection('MyCollection').find({address: {$in: cities }}).toArray(function(err,cursor){ console.log(err,cursor);
});

mariohmol avatar Feb 24 '16 14:02 mariohmol

@mariohmol but do you have to have 'MyCollection' defined as a model within LoopBack? what if you don't want to do that, and don't want to open another connection to Mongo?

AndersonZacharyT avatar Aug 19 '16 19:08 AndersonZacharyT

sorry.. was testing and do not have this project anymore

mariohmol avatar Aug 20 '16 00:08 mariohmol

For anyone else looking at this, you want MyCollection.app.dataSource.connector.connect or alternatively MyCollection.app.dataSource.adapter.connect. The connect method on the dataSource is a generic one and returns the empty params that @mariohmol and @thiagobrunoms encountered.

bitmage avatar Nov 02 '16 05:11 bitmage

I am using this

app.dataSources.mongoConnector.connect(function (errconnect, mdb) {
});

I am getting connected but it returns mdb undefined. My datasource is

"mongoConnector": {
   "url": "${CONNECTION_STRING}",
   "name": "mongoConnector",
   "connector": "mongodb"
 }

Anyone can tell me what am i missing here.

akvaliya avatar Nov 13 '17 13:11 akvaliya

I am getting error message - " Cannot read property 'connect' of undefined". Is there any way to add a hook when datasource is connected to trigger any event ? app.dataSources.mongods.connect(function(err,db) {

mongods is datasource name.

aravindatcts avatar Dec 09 '17 18:12 aravindatcts

app.dataSources.hmsDs.connector.connect(function(err,db){ console.log(db); db.collection('mastercounters').findOne({},function(err,doc){ console.log(doc) }); }); it is working example : hmsDs is datasource name defined in datasource.json

uds214125 avatar Jan 17 '18 12:01 uds214125

I am using the same method but i am getting "db" object undefined.

akvaliya avatar Jan 18 '18 08:01 akvaliya

@uds214125 Thanks. I was missing connector after ds name. Working now.

akvaliya avatar Jan 19 '18 16:01 akvaliya