Javascript $lookup结果显示[对象]

Javascript $lookup结果显示[对象],javascript,node.js,mongodb,Javascript,Node.js,Mongodb,使用带有nodejs和mongo/mongoose的passport 我将考勤记录存储在一个集合中,该集合使用req.user.id作为查找,将考勤记录与注册用户关联起来。密钥字段称为“userId” 在尝试连接时,我了解到users集合中的_id不是string类型。我一直在努力解决这个问题。我的最新任务是在将req.user.id转换为ObjectId()后尝试保存考勤记录。不过,运气不好。看起来用户ID仍保存为字符串。我怎样才能让这个加入生效 router.post('/', ensure

使用带有nodejs和mongo/mongoose的passport

我将考勤记录存储在一个集合中,该集合使用req.user.id作为查找,将考勤记录与注册用户关联起来。密钥字段称为“userId”

在尝试连接时,我了解到users集合中的_id不是string类型。我一直在努力解决这个问题。我的最新任务是在将req.user.id转换为ObjectId()后尝试保存考勤记录。不过,运气不好。看起来用户ID仍保存为字符串。我怎样才能让这个加入生效

router.post('/', ensureAuthenticated, function(req, res){

    var query = { userId: req.user.id };

    var update = {
        userId: new ObjectId(req.user.id),  // This is where I need to save
        response: req.body.inOrOut,
        notes: req.body.notes
    };

    //  Upsert
    var options = { upsert: true };

    // Use mongoose to save via upsert.
    RSVP.findOneAndUpdate( query, update, options, function( err, doc ) {
            if ( err ) throw err;
    });
以下是加入:

RSVP.aggregate([{
        $lookup: {
            from: "users", // collection name in db
            localField: "userId",
            foreignField: "_id",
            as: "user"
        }
    }
    ]).exec( (err, rsvpList) => {
        if (err) throw err;
        console.log(rsvpList);
    }); 
编辑:

我刚刚意识到我的rsvps的mongoose模式仍然有userId作为字符串。我将其更改如下:

let mongoose = require('mongoose');
//let ObjectId = require('mongodb').ObjectId;

// RSVP Schema
var RSVPSchema = mongoose.Schema({
    userId: {
        //type: String,
        type: mongoose.Schema.Types.ObjectId,
        index:true
    },
    response: {
        type: String
    },
    notes: {
        type: String
    },
});

var RSVP = module.exports = mongoose.model('RSVP', RSVPSchema);
My console.log:

console.log('rsvp: ' + rsvpList[0].user.toString());

它显示[对象]。我看不出我的加入是否有效。如何测试rsvpList[0]。user以查看它是否包含已加入的用户?

我已接近。应该是:

console.log('rsvp: ' + rsvpList[0].user[0].name);

JavaScript 101
console.log()
不“深度序列化”对象。使用
JSON.stringify
作为