【问题标题】:Get data from MongoDB with NodeJS使用 NodeJS 从 MongoDB 获取数据
【发布时间】:2016-06-03 00:33:29
【问题描述】:

我是 MongoDB 和 NodeJS 开发的新手。我已经阅读了我发现的所有内容,并且可能搜索错误。 请帮帮我!

我有这个数据库 - 名称:商店,文档:类别,产品

这是 db.collection('categories');

{
"_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) {
	var _         = require("underscore");
	// var parseSchema = require('mongodb-schema');
	var mdbClient = require('mongodb').MongoClient;

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

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


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

我需要在变量中设置:

var categories = //所有类别,例如:男装、女装

var subCategories = //所有子类别例如:服装等

到达每个变量的正确方法是什么?

谢谢!

【问题讨论】:

    标签: node.js mongodb


    【解决方案1】:

    字段演示:

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

    类似地,您可以对其他字段执行此操作

    【讨论】:

    • 谢谢!这有助于另一部分,但无助于放置我需要的所有内容这是我的代码,用于获取子类别的所有类别和类似内容,但我需要使其更可靠 collection.find().toArray(function(错误,项目){ items.forEach(函数(项目,索引){类别[索引] = { id:item.id,名称:item.name,图像:item.image,page_desc:item.page_description,page_title:item。 page_title } }); });
    • 对于子类别:collection.find().toArray(function(err, items) { items.forEach(function (item, index_x) { item.categories.forEach(function (subItem, index_y){ subCategory[index_x [index_y]] = subItem.name; }); });
    • 谁能解释我一些问题在“header.ejs”中我有一个带有这个代码的img标签。 > 为什么图片不显示?这是正确的地址!!!仅在 Mozilla Firefox 中,如果我将“../public/images/”更改为“..\public/images/”,则会显示图像
    猜你喜欢
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 2021-12-19
    • 2019-12-27
    相关资源
    最近更新 更多