0

So why are the object's variables different in the debugger debugger picture

and the console log? I'm inquiring especially about the _id variable which looks very different!

In the debugger, I'm unable to find this part: "597e874b52fba6324c9ac192"!

[ { _id: 597e874b52fba6324c9ac192,
title: 'x',
author: 'z',
body: 'y',
date: 2017-07-31T01:26:35.533Z,
comments: [],
__v: 0 },
{ _id: 597e87660726ab322c303a8e,
title: 'x',
author: 'z',
body: 'y',
date: 2017-07-31T01:27:02.266Z,
comments: [],
__v: 0 },
{ _id: 597e8773c45264303c3fcf0b,
title: 'x',
...

Here is the node JS code I used:

function test() {

var mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/test");
var Schema = mongoose.Schema;

var blogSchema = new Schema({
    title: String,
    author: String,
    body: String,
    comments: [{body: String, date: Date}],
    date: {type: Date, default: Date.now},
    hidden: Boolean,
    meta: {
        votes: Number,
        favs: Number
    }
});

var Blog = mongoose.model('Blog', blogSchema);




    Blog.create({title: 'x', author: "z", body: "y"}, function (err, small) 
{
        if (err) return handleError(err);
        // saved!
    });
    var weirdResults;
    Blog.aggregate([
        {$match: {author: {$exists: true}}}
    ], function (err, results) {
        weirdResults = results;
        if (err) return next(err);
        console.log(weirdResults);
    });

}

test();

Thanks all!

Emilio
  • 1,314
  • 2
  • 15
  • 39
  • 2
    Says `_id: ObjectID` consistently in your debugger output. There's also a little arrow to "expand" which would show you the value. Just like any JavaScript object. `console.log()` basically calls the `.toString()` prototype and "stringifies" the object content. That's why one shows the value directly and the other requires you to "expand" the object in order to inspect the value. – Neil Lunn Jul 31 '17 at 02:33
  • @NeilLunn if I expend the arrow, there's nothing even close to the `_id` shown in the console – Emilio Jul 31 '17 at 02:55
  • 1
    @Emilio Well what *does* it show when you expand it? But yes, mongo object ids are [pretty magical](https://stackoverflow.com/questions/13104690/nodejs-mongodb-object-id-to-string) – Bergi Jul 31 '17 at 03:13
  • 1
    Take it up with the authors of the remote debugger. It's actually a "mongoose" representation of an `ObjectId`, which also like any actual "mongoose document object" ( as returned by `.find()` ) as opposed to a "plain JavaScript object" ( as returned by `.aggregate()` ) has "all sorts of weird and wonderful internal properies". This is generally countered by the standard JavaScript protypes of `.toObject()` or `.toString()`. How someones tool implements those is "their business". But I did explain what `console.log()` actually does and why it's different. – Neil Lunn Jul 31 '17 at 03:16

1 Answers1

0

You can get id by using _id.toString();

Ankit Manchanda
  • 562
  • 6
  • 21