Please note that the login password for the site is the same as for the members area:.
Q:
Get all the documents from a collection in mongodb
My app is using express and node.js. I am using MongoDB. I have an database with 2 collections called "attributes" and "products".
I want to get all the attributes from the attributes collection but I'm having some problems.
In the "products" collection, the attributes have the following fields: "Name" and "ID".
I want to get all the attributes from the attributes collection. I have the following code:
var Db = require('mongoose');
var db = new Db('measurement', new Db.StoreConfig("localhost", 27017, {auto_reconnect: true}), {w:1});
var res = db.collection("attributes").find({"Name": {"$exists": true}});
console.log(res);
The code above, prints out this:
{ _id: 5d281751f9e7faca941c5e04, Size: 10, Name: 'Height'}
{ _id: 5d28177a9e7faca941c5e051, Size: 3, Name: 'Width'}
{ _id: 5d28177c9e7faca941c5e052, Size: 2, Name: 'Weight'}
{ _id: 5d2817849e7faca941c5e053, Size: 4, Name: 'Age'}
It's great! Now what I want to do is to get all the "Name" from the attributes collection. The code below doesn't print anything:
var Db = require('mongoose');
var db = new Db('measurement', new Db.StoreConfig("localhost", 27017, {auto_reconnect: true}), {w:1});
var res = db.collection("attributes").find({"Name": {"$exists": true}}).map(function(doc) {
return doc.Name;
});
console.log(res);
Why?
A ac619d1d87
Related links:
Comments