0

Im new to mongodb and node js so please excuse me if this is very simple. So I have a schema:

var mongoose = require('mongoose');

var CatSchema = new mongoose.Schema({
  cat_name: String,
  cat_value: Number
});

module.exports = mongoose.model('Cat', CatSchema);

And when I add data to my dbs, it looks like this:

> db.cats.find()
{ "_id" : ObjectId("..."), "cat_name" : "test1", "__v" : 0, "cat_value" : 55 }
{ "_id" : ObjectId("..."), "cat_name" : "test2", "cat_value" : 24, "__v" : 0 }
{ "_id" : ObjectId("..."), "cat_name" : "test1", "__v" : 0, "cat_value" : 70 }

I want to call the dbs and create an array of cat_names for each document in the collection.

So the final effects looks like this:

var cat_names = [test1, test2, test1 ... ]

Any idea how to do this? I tried using the foreach loop, db.collection('cats') however I just cannot figure it out.

1
  • 1
    if we use distinct it will not print cats with same name. Commented Aug 17, 2016 at 12:57

4 Answers 4

1

This works for me: var cat_names = [];

Cat.find(function(err, data){
    if(err){            
        console.log(err);
    }
    var stringify = JSON.stringify(data)
    content = JSON.parse(stringify);

    content.forEach(function(result){
        cat_names.push(result.cat_name);
    })

    console.log(cat_names);
})
Sign up to request clarification or add additional context in comments.

3 Comments

What are the JSON.stringify and JSON.parse calls for?
Otherwise you get undefined returned when cat_names is called
Hmm...that doesn't make sense. You can skip those calls and just do data.forEach(....
1

You could use exec, this will allow you to return an array of the JSON docs you retrieved.

db.cats.find(function(err, cats){
    var arr_docs = cats.map(function(cat) { return cat.cat_name});
    if(err){
       res.json(err)
    } else {
       res.json(arr_docs)
    }
})

1 Comment

I specifically want the cat names back only
0
var cat_names = [];

    db.cats.find(function(err, data){
        if(err){            
            console.log(err);
        }

        data.forEach(function(result){
            cat_names.push(result.cat_name);
        })

        console.log(cat_names);
    })

I have tested this code and is working fine for your requirement.

5 Comments

Hmm... I get the following :'[ undefined, undefined, undefined ]' Any idea why?
my bad, i made a silly mistake, this answer is now updated, please check again. I used forEach so that you can understand it better.
One more thing, i fyou have any confusion, just console.log(data) first in order to check whether the query is working fine or not.
Still getting undefined back though
I tested this code on my local server, and it worked fine for me, though i was using different schema but i got the results as you desired, can you do one thing, just console.log(data), it will give you a clear picture.
0
   Cat
  .find()
  .then(function(cat){
   var cat_Names_Array = [];
    cat.map(function(value){
      cat_Names_Array.push(value.cat_name);
    });

    return cat_Names_Array;  
    })
     .catch(function(err){
      console.log('err in finding:',err);
    });

I hope it will solve your query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.