13

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 ?

2
  • 1
    Have you tried flattening the result using toObject() like var participants = docs.map(function(doc) { return doc.toObject().user_id; }) or the lean() method which returns plain JS objects?
    – chridam
    Sep 17, 2015 at 15:09
  • 1
    The issue was that mongoose doesn't return objects as they are in the database but actually returns objects wrapped inside objects proper to mongoose.
    – Hugo
    Sep 17, 2015 at 15:39

4 Answers 4

25

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.

4
  • 1
    The proper solutions is adding the property to the Schema and Masadow's bypass is a functional alternative.
    – Hugo
    Sep 17, 2015 at 15:35
  • Thank you for this answer, it helped me. But for the ._id, How can I access it, its not in my Schema, It returns undefind?
    – Meir
    Jun 25, 2020 at 20:54
  • YES! This! It wasn't in the schema. so it was showing up on a full print of the Object, but not when accesing the key! Thank you!
    – Julian E.
    Jul 13, 2020 at 11:10
  • " suspect the value you are trying to get is not in your Schema but is stored in your database." this line saves my day.. the FIELD IS NOT ON SCHEMA! Aug 20, 2021 at 20:24
22

Mongoose does funky stuff when it comes to accessing model properties. Your best bet whenever you're having problems, is either to use .lean() as part of your query, or call .toObject() on the output to convert the model into a plain JS object.

e.g. using .toObject()

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

e.g. using lean()

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

The above find() returns the array of documents from DB

You can access the value of each record

docs.map((d)=>{
    console.log(d.get('user_id'))
})
1
  • And in case you only want to access one document, just use the findOne method instead of the find method. Sep 11, 2021 at 15:38
0

I had the same issue, the resolution is really simple, instead of find use findOne, because find can return many results and thats why when you try to go to the property of object it says undefined. If you use findOne, the query will definately have one result and then you would be able to use the item name and get the answer ex: object.item, If you want to use Model.find(), then you would need to access the first array in the result and then use it property, ex: object[0].item.

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Nov 8, 2022 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.