mongoose Aggregation.lookup is not working as expected

3,263 views
Skip to first unread message

Yaragalla Muralidhar

unread,
Nov 5, 2016, 2:00:07 AM11/5/16
to mongodb-user
The following is my code in App.js. I am using mongoose for communication with mongodb. I am using Mongoose aggregation.lookup for joining two tables and i am not able to get the collection of records. Can somebody tell me what is the problem?

App.js

var catProds = mongoose.model('SV_Catgeory_Products');
var prods = mongoose.model('SV_Products');
var obsr=catProds.aggregate([
   
{
        $lookup
:
         
{
           
from: "prods",
            localField
: "productId",
            foreignField
: "productId",
           
as: "inventory_docs"
         
}
     
}
 
])
  console
.log(obsr);

The following is the output:-

Aggregate {
  _pipeline
: [ { '$lookup': [Object] } ],
  _model
:
   
{ [Function: model]
     hooks
: Kareem { _pres: {}, _posts: {} },
     
base:
     
Mongoose {
        connections
: [Object],
        plugins
: [],
        models
: [Object],
        modelSchemas
: [Object],
        options
: [Object] },
     modelName
: 'SV_Catgeory_Products',
     model
: [Function: model],
     db
:
     
NativeConnection {
       
base: [Object],
        collections
: [Object],
        models
: [Object],
        config
: [Object],
        replica
: false,
        hosts
: null,
        host
: 'localhost',
        port
: 27017,
        user
: undefined,
       
pass: undefined,
        name
: 'satvik',
        options
: [Object],
        otherDbs
: [],
        _readyState
: 2,
        _closeCalled
: false,
        _hasOpened
: false,
        _listening
: false,
        db
: [Object],
        _events
: [Object],
        _eventsCount
: 1 },
     discriminators
: undefined,
     schema
:
     
Schema {
        obj
: [Object],
        paths
: [Object],
        subpaths
: {},
        virtuals
: [Object],
        singleNestedPaths
: {},
        nested
: {},
        inherits
: {},
        callQueue
: [Object],
        _indexes
: [],
        methods
: {},
        statics
: {},
        tree
: [Object],
        _requiredpaths
: undefined,
        discriminatorMapping
: undefined,
        _indexedpaths
: undefined,
        query
: {},
        childSchemas
: [],
        s
: [Object],
        options
: [Object],
       
'$globalPluginsApplied': true },
     collection
:
     
NativeCollection {
        collection
: null,
        opts
: [Object],
        name
: 'sv_catgeory_products',
        collectionName
: 'sv_catgeory_products',
        conn
: [Object],
        queue
: [],
        buffer
: true,
        emitter
: [Object] },
     
Query: { [Function] base: [Object] },
     
'$__insertMany': [Function],
     insertMany
: [Function] },
  options
: undefined }



Kevin Adistambha

unread,
Nov 13, 2016, 8:40:12 PM11/13/16
to mongodb-user

Hi,

I am using mongoose for communication with mongodb. I am using Mongoose aggregation.lookup for joining two tables and i am not able to get the collection of records. Can somebody tell me what is the problem?

It looks like you didn’t provide a callback to the aggregate() call. Without a callback, it will return the Aggregate object instead. To execute the aggregation, you can either:

Please note that for $lookup, you need to know the collection name as stored by MongoDB. Please see http://mongoosejs.com/docs/guide.html#collection for Mongoose collection name options and default.

If you are still having issues, could you post:

  • Your MongoDB version.
  • Your Mongoose version.
  • What you have tried so far, and any error messages.

Best regards,
Kevin

kyse mon mon

unread,
Feb 24, 2017, 5:36:46 PM2/24/17
to mongodb-user
hi do you have one mongoose lookup example source ?

Kevin Adistambha

unread,
Mar 1, 2017, 6:23:07 PM3/1/17
to mongodb-user

Hi

hi do you have one mongoose lookup example source ?

Please take a look at the example in the mongoose documentation: http://mongoosejs.com/docs/api.html#aggregate_Aggregate-lookup

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages