Loopback: Question - How to use Mongodb GridFS with loopback?

Created on 21 Sep 2014  路  14Comments  路  Source: strongloop/loopback

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();
      });

All 14 comments

I found it.

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

:+1:

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!

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

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.

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 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?

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

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.

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.

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.

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

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

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

Was this page helpful?
0 / 5 - 0 ratings