Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm running this code on node.js

var mongoose = require('mongoose');
mongoose.model('participant',new mongoose.Schema({},{ collection : 'forumParticipant' }));
var Participant = mongoose.model('participant');
mongoose.connect('******');

Participant.find({entity_id: 0}, function (err, docs) {
   console.log(docs[0]);
   console.log(docs[0].entity_id)
});

1) The first console.log return the full document

2) The second console.log return undefinied

I don't understand why.

I need to perform something like

var participants = docs.map(function(d){return d.user_id})

How can I achieve that? What am I missing ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
531 views
Welcome To Ask or Share your Answers For Others

1 Answer

I suspect the value you are trying to get is not in your Schema but is stored in your database.

You have two solutions from there. You can either add entity_id to your Schema and Mongo will be able to bind it to the Document object you receive. This is the recommended way.

Or you can bypass mongoose Schema and access the raw document stored in the database with docs[0]._doc.entity_id. I don't recommend this solution unless you know what you're doing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...