Moongoose: Find name in another selection

Im trying to get the name of a user by looking in up with a id.

query.find(function(err, dat) {
  for(i in dat) {
    users.findOne({_id: dat[i].owner}, function(userErr, user) {
      dat[i].ownerName = user.name;
      if(parseInt(i)+1==dat.length) res.render('view', {local: dat});
    });
  }
});

Hope you guys understand. Im trying to get a name from another selection in a query. But the problem is, that I cant save a variable outside the user query callback. How do i do this the right way? :-)

At the example a post above i only get the last one in the query.

If the document you want to get from the innder ( second ? ) find is related in some way with the first query's documents, I suggest you define a DBRef-like relation in your schema or models and use populate() like shown in the mongoose documentation

Quick example:

Let's say you want to find all Posts talking about caffeine, whose author is the currently logged in user. First, define your models:

var mongoose = require('mongoose')
  , Schema = mongoose.Schema

var UserSchema = new Schema({
    name    : String
  , email   : Number
  , posts   : [{ type: Schema.ObjectId, ref: 'Post' }]
});

var PostSchema = new Schema({
    _author : { type: Schema.ObjectId, ref: 'User' }
  , title    : String
  , body     : String
});

var Post = mongoose.model('Post', PostSchema);
var User = mongoose.model('User', UserSchema);

.. then you can:

Post.find({title: /caffeine/i}).populate('_author', null, {name: user.name}).run(function (err, posts) {
 res.render('view', {local: posts})
});