2016-06-02 19 views
0

MongoDBとNodeJS開発の新機能です。私は私が見つけたすべてを読んで、おそらく間違って検索しています。 私を助けてください!NodeJSでMongoDBからデータを取得

私はこのデシベル持っている - 名前:ショップ、ドキュメント:カテゴリ、製品

これはdb.collectionです( 'カテゴリ');

{ 
 
"_id" : { "$oid" : "5172d1daffdd81f3234d5f88" }, 
 
     "categories" : [ { 
 
     "categories" : [ { 
 
     "id" : "mens-clothing-suits", 
 
       "image" : "categories/mens-clothing-suits.jpg", 
 
       "name" : "Suits", 
 
       "page_description" : "Shop Men's suits for business or pleasure. Enjoy from a variety of different styles and cuts.", 
 
       "page_title" : "Mens Suits for Business and Casual", 
 
       "parent_category_id" : "mens-clothing", 
 
       "c_showInMenu" : true 
 
     }, 
 
     { 
 
     "id" : "mens-clothing-jackets", 
 
       "image" : "categories/mens-clothing-sportscoats.png", 
 
       "name" : "Jackets & Coats", 
 
       "page_description" : "Shop Men's Jackets, Coats & Outerwear. Classic outdoor-tested garments with traditional styling details that provide comfort, insulation and ease of movement, whatever the weather.", 
 
       "page_title" : "Men's Jackets Including Jackets & Blazzers", 
 
       "parent_category_id" : "mens-clothing", 
 
       "c_showInMenu" : true 
 
     }, 
 
     { 
 
     "id" : "mens-clothing-dress-shirts", 
 
       "image" : "categories/mens-clothing-dress-shirts.jpg", 
 
       "name" : "Dress Shirts", 
 
       "page_description" : "Shop Men's dress shirts in a variety of colors and styles including striped, button down, non-iron & more", "page_title" : "Men's Dress Shirts including Striped, Button Down, Non-Iron & More", 
 
       "parent_category_id" : "mens-clothing", 
 
       "c_showInMenu" : true 
 
     }, 
 
     { 
 
     "id" : "mens-clothing-shorts", 
 
       "image" : "categories/mens-clothing-shorts.png", 
 
       "name" : "Shorts", "page_description" : "Shop Men's spring shorts in cotton. Variety of different fits.", 
 
       "page_title" : "Men's Spring Shorts", 
 
       "parent_category_id" : "mens-clothing", 
 
       "c_showInMenu" : true 
 
     }, 
 
     { 
 
     "id" : "mens-clothing-pants", 
 
       "image" : "categories/mens-clothing-pants.png", 
 
       "name" : "Pants", 
 
       "page_description" : "Shop Men's Trousers. Practical, easy-to-wear styles wherever you're headed. Check out famous rugged, long-lasting trousers, jeans, cargo pants and more.", 
 
       "page_title" : "Men's Pants Including Khakis, Cargos, Trousers, Jeans & More", 
 
       "parent_category_id" : "mens-clothing", 
 
       "c_showInMenu" : true 
 
     } ], 
 
       "id" : "mens-clothing", 
 
       "image" : "categories/mens-clothing-accessories.jpg", 
 
       "name" : "Clothing", 
 
       "page_description" : "Shop Men's Clothing. Relaxed, timeless classics you can rely on; from denim to corduroys and sweaters to shirts. Huge range of contemporary colours and eco-aware designs: great casualwear.", 
 
       "page_title" : "Mens Clothing Including Suits, Tops, Bottoms & More", 
 
       "parent_category_id" : "mens", 
 
       "c_showInMenu" : true 
 
     }, 
 
     { 
 
     "categories" : [ { 
 
     "id" : "mens-accessories-ties", 
 
       "image" : "categories/mens-accessories-ties.png", 
 
       "name" : "Ties", "page_description" : "Shop Mens's Ties for all occasions including business or casual.", 
 
       "page_title" : "Men's Casual and Business Ties", 
 
       "parent_category_id" : "mens-accessories", 
 
       "c_showInMenu" : true 
 
     }, 
 
     { 
 
     "id" : "mens-accessories-gloves", 
 
       "name" : "Gloves", 
 
       "page_description" : "Shop Men'sGloves. Versatile, commuter, boot, oxford, deer and resolve gloves. All with famous long-lasting quality.", 
 
       "page_title" : "Men's Gloves", 
 
       "parent_category_id" : "mens-accessories", 
 
       "c_showInMenu" : true 
 
     }, 
 
     { 
 
     "id" : "mens-accessories-luggage", 
 
       "image" : "categories/mens-accessories-luggage.jpg", 
 
       "name" : "Luggage", 
 
       "page_description" : "Shop Men's Wheeled Luggage. Versatile, rugged suitcases, baggage, holdalls and shoulder bags. All with famous long-lasting quality.", 
 
       "page_title" : "Men's Wheeled Luggage", 
 
       "parent_category_id" : "mens-accessories", 
 
       "c_showInMenu" : true 
 
     } ], 
 
       "id" : "mens-accessories", 
 
       "name" : "Accessories", 
 
       "page_description" : "Shop mens accessories including belts, wallets. gloves, hats, watches, luggage & more.", 
 
       "page_title" : "Men's Accessories Belts, Wallets. Gloves, Hats, Watches, Luggage & More", 
 
       "parent_category_id" : "mens", 
 
       "c_showInMenu" : true 
 
     } ], 
 
     "id" : "mens", 
 
     "name" : "Mens", 
 
     "page_description" : "Men's range. Hard-wearing boots, jackets and clothing for unbeatable comfort day in, day out. Practical, easy-to-wear styles wherever you're headed.", 
 
     "page_title" : "Men's Footwear, Outerwear, Clothing & Accessories", 
 
     "parent_category_id" : "root", 
 
     "c_showInMenu" : true 
 
}

と私が持っているNodeJS用 - >

exports.index = function(req, res) { 
 
\t var _   = require("underscore"); 
 
\t // var parseSchema = require('mongodb-schema'); 
 
\t var mdbClient = require('mongodb').MongoClient; 
 

 
mdbClient.connect("mongodb://localhost:27017/shop", function(err, db) { 
 
\t \t var collection = db.collection('categories'); 
 

 
\t \t collection.find().toArray(function(err, items) { 
 
\t \t \t res.render("index", { 
 
\t \t \t // \t // Underscore.js lib 
 
\t \t \t \t _  : _, 
 
\t \t \t \t 
 
\t \t \t // \t // Template data 
 
       title : "Hello World!", 
 
\t \t \t \t items : items 
 
\t \t \t }); 
 

 

 
\t \t \t db.close(); 
 
\t \t }); 
 
}); 
 
\t console.dir("Called findOne!"); 
 
};

私は変数に設定する必要があります。

VARカテゴリ= //アル例えばリットルカテゴリ:メンズ、レディース

VARのサブカテゴリ= //例えばすべてのサブカテゴリを:衣料など

各変数に到達する権利の方法は何ですか?

ありがとうございました!フィールドの

答えて

1

デモ:

db.getCollection('categories').find(
     {"categories.0.categories.image":"categories/mens-clothing-suits.jpg"}, 
     {"categories.categories.image":1}); 

同様に、あなたが他のフィールド

+0

のためにこれを行うことができますが、ありがとうございました! これは別の部分に貢献したが、私はこれは私のすべてのカテゴリを取得するためのコードとサブカテゴリについても同様である が必要なもののすべてのコンテンツを置くには助けにはならなかったが、私は(それがより信頼性の高い collection.findようにする必要があり).toArray(関数(ERR、項目){ \t \t \t items.forEach(関数(項目、インデックス){ \t \t \t \tカテゴリ[インデックス] = { \t \t \t \t \t ID:item.id、 \t \t \t \t \t名:item.name、 \t \t \t \t \t画像:item.image、 \t \t \t \t \t page_desc:item.page_description、 \t \t \t \t \t PAGE_TITLE:item.page_title \t \t \t \t} \t \t \t}); \t \t});サブカテゴリーの –

+0

collection.find()。toArray(function(err、items){ \t \tの項目です。forEachの(関数(項目、index_x){ \t \t \t item.categories.forEach(関数(サブアイテム、index_y){ \t \t \t \tサブカテゴリ[index_x [index_y] = subItem.name; \t \t \t \t \t} ); \t \t \t}); –

+0

M喜んで助けた!!!!!!! –

関連する問題