question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Populate does not work with cursor(), but works with exec()

See original GitHub issue

I’m running Mongoose 4.4.20 with Mongo 3.0.12.

These are the relevant parts of my schema:

var ObjectId = mongoose.Schema.Types.ObjectId
var campaignSchema = mongoose.Schema({
  // ...
  summary: { type: ObjectId, ref: 'summaries' },
  // ...
}

var summarySchema = mongoose.Schema({
  // ...
}

var Campaign = mongoose.model('Campaign', campaignSchema, 'campaigns')
var Summary = mongoose.model('Summary', summarySchema, 'summaries')

If I use regular exec, it works just fine:

Campaign.find({
   ...
})
  .populate('summary')
  .exec()
  .then(function (data) {
    console.log(data)
  })

However, if I try the same with cursor, it does not work: the summary field is not populated:

var cursor = Campaign.find({
   ...
})
  .populate('summary')
  .cursor()

cursor.next().then(function (data) {
  console.log(data)
})

The code above only shows the refs’ ObjectId instead of the full document.

Edit

I’m really sorry, there might be an error when I copy-pasted the code. That extra .exec does not exist.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
vkarpov15commented, Jun 7, 2017

@zoellner opened up a new issue #5334 to track, keeping this issue closed ☝️

0reactions
zoellnercommented, Jun 1, 2017

I am seeing the same issue. code to reproduce below. There are a few options to populate and I’m not seeing a difference. It seems that the model option is ignored

link1 is populated as expected (there is no difference when changing the model options, no error when using the model: 'asdf' option) link2 is always null after the population when using cursor

code to reproduce:

'use strict';

const mongoose = require('mongoose');
const _h = require('highland');

mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/mongoose-gh-4559');

const LinkedSchema = new mongoose.Schema({
  name: {type: String}
});

const Linked = mongoose.model('Linked', LinkedSchema);

const MainSchema = new mongoose.Schema({
  link1: {type: mongoose.Schema.Types.ObjectId, ref: 'Linked'},
  link2: {type: mongoose.Schema.Types.ObjectId}
});

const Main = mongoose.model('Main', MainSchema);

Linked.remove({}, () => {
  Main.remove({}, () => {
    Linked.create({name: 'link1'}, (err, link1) => {
      Linked.create({name: 'link2'}, (err, link2) => {
        Main.create({link1: link1._id, link2: link2._id}, () => {
          console.log('cursor');
          _h(Main.find({})
            //use string for Model
            .populate({path: 'link1', model: 'Linked'})
            .populate({path: 'link2', model: 'Linked'})

            //use reference to Model
            // .populate({path: 'link1', model: Linked})
            // .populate({path: 'link2', model: Linked})

            //use invalid model
            // .populate({path: 'link1', model: 'asdf'})
            // .populate({path: 'link2', model: 'asdf'})
            .lean()
            .cursor()
          )
          .tap(_h.log)
          .done(() => {
            console.log('no cursor');
            Main.find({})
            .populate({path: 'link1', model: 'Linked'}) //also works with model: Linked
            .populate({path: 'link2', model: 'Linked'}) //also works with model: Linked
            .lean()
            .exec((err, docs) => {
              console.log(docs);
              process.exit();
            });
          });
        });
      });
    });
  });
});

code output:

cursor
{ _id: 593064f0ee054b339b8a5165,
  link1: { _id: 593064f0ee054b339b8a5163, name: 'link1', __v: 0 },
  link2: null,
  __v: 0 }
no cursor
[ { _id: 593064f0ee054b339b8a5165,
    link1: { _id: 593064f0ee054b339b8a5163, name: 'link1', __v: 0 },
    link2: { _id: 593064f0ee054b339b8a5164, name: 'link2', __v: 0 },
    __v: 0 } ]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose populate works with exec() but not with cursor()
summary field is not populated, returning only the ObjectId referenced by the model. Edit. Here are the relevant parts of my schemas: var...
Read more >
Mongoose populate works with exec() but not with cursor()
Coding example for the question Mongoose populate works with exec() but not with cursor()-mongodb.
Read more >
Working with cursors and dynamic queries in PL/SQL
When you are fetching a single row, use SELECT-INTO or EXECUTE IMMEDIATE-INTO (if your query is dynamic). Do not use an explicit cursor...
Read more >
ORM Querying Guide - SQLAlchemy 1.4 Documentation
The select() construct accepts ORM entities, including mapped classes as well as class-level attributes representing mapped columns, ...
Read more >
Faster Mongoose Queries With Lean
In this tutorial, you'll learn more about the tradeoffs of using lean() . ... that the Person model's getters and virtuals don't run...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found