Node.js Mongodb查询嵌套对象

Node.js Mongodb查询嵌套对象,node.js,mongodb,pug,express,Node.js,Mongodb,Pug,Express,我的目标是 { "Team-A": { "captain": "A", "homeGround": "some ground", "winLoss": "12-4-0" }, "Team-B": { "captain": "B", "homeGround": "some ground 2", "winLoss": "4-4-4" } } 我需要将此信息显示为 <h

我的目标是

{
   "Team-A": {
        "captain": "A",
        "homeGround": "some ground",
        "winLoss": "12-4-0"
    },
    "Team-B": {
        "captain": "B",
        "homeGround": "some ground 2",
        "winLoss": "4-4-4"
    }
}
我需要将此信息显示为

<h1>Team-A</h1>
<h2>some ground</h2>
<h3>12-4-0</h3>

<h1>Team-B</h1>
<h2>some ground2</h2>
<h3>4-4-4</h3>
翡翠:

MongoClient.connect("mongodb://localhost:27017/MY_DB_TEST", function(err, db) {
      if(!err) {
        console.log("We are connected");        

        var collection =  db.collection('football_teams');      
        var stream = collection.find().stream();

        stream.on("data", function(item){
            console.log(item);
            res.render('index', {obj: item });
        });     
      }
    });
h1= obj.captain
h2= obj.homeGround
h3= obj.winLoss
我的控制台输出是:

{ _id: 51064fa1e0d5d118b4b29aa0,
  'Team-A': {
        'captain": 'A',
        'homeGround": 'some ground',
        'winLoss': '12-4-0'
    } }

title is not defined
    at eval (eval at <anonymous> (C:\node_modules\jade\lib\jade.js:176:8))
    at exports.compile (C:\node_modules\jade\lib\jade.js:181:12)
    at Object.exports.render (C:\node_modules\jade\lib\jade.js:216:14)
    at View.exports.renderFile [as engine] (C:\node_modules\jade\lib\jade.js:243:13)
    at View.render (C:\node_modules\express\lib\view.js:75:8)
    at Function.app.render (C:\node_modules\express\lib\application.js:503:10)
    at ServerResponse.res.render (C:\node_modules\express\lib\response.js:721:7)
    at CursorStream.exports.index (C:\Users\HFR&D\Desktop\nodemongoexpress\routes\index.js:18:8)
    at CursorStream.EventEmitter.emit (events.js:96:17)
    at CursorStream._onNextObject (C:\Users\HFR&D\Desktop\nodemongoexpress\node_modules\mongodb\lib\mongodb\cursorstream
{u id:51064fa1e0d5d118b4b29aa0,
“A队”:{
“船长”:“A”,
“主场”:“一些主场”,
“winLoss”:“12-4-0”
} }
标题未定义
评估时(评估时为(C:\node\u modules\jade\lib\jade.js:176:8))
在exports.compile(C:\node\u modules\jade\lib\jade.js:181:12)
在Object.exports.render(C:\node\u modules\jade\lib\jade.js:216:14)
在View.exports.renderFile[作为引擎](C:\node\u modules\jade\lib\jade.js:243:13)
在View.render(C:\node\u modules\express\lib\View.js:75:8)
位于Function.app.render(C:\node\u modules\express\lib\application.js:503:10)
在ServerResponse.res.render(C:\node\u modules\express\lib\response.js:721:7)
在CursorStream.exports.index(C:\Users\HFR&D\Desktop\nodemonogexpress\routes\index.js:18:8)
在CursorStream.EventEmitter.emit上(events.js:96:17)
位于CursorStream.\u onNextObject(C:\Users\HFR&D\Desktop\nodemongoexpress\node\u modules\mongodb\lib\mongodb\CursorStream
我不明白我应该如何循环通过我的对象并获得Team-A/Team-B的值及其各自的详细信息

如何使用
nodejs
jade
实现这一点

更新:

MongoClient.connect("mongodb://localhost:27017/MY_DB_TEST", function(err, db) {
      if(!err) {
        console.log("We are connected");        

        var collection =  db.collection('football_teams');      
        var stream = collection.find().stream();

        stream.on("data", function(item){
            console.log(item);
            res.render('index', {obj: item });
        });     
      }
    });
h1= obj.captain
h2= obj.homeGround
h3= obj.winLoss
假设有20个团队,我的集合将由20个文档组成。我想做的是,循环我的集合并在20个文档中显示信息,如上所述

  • 我的数据库结构正确吗?我应该为每个团队提供不同的文档吗

  • 如果是,在jade中渲染时如何动态获取文档标题(team-A、team-B)


您确实可以在console.log中看到Team-A集合

{ _id: 51064fa1e0d5d118b4b29aa0,
  'Team-A': {
        'captain": 'A',
        'homeGround": 'some ground',
        'winLoss': '12-4-0'
    } }
您得到的错误是
标题未定义
,因此我猜是其他东西破坏了您的代码。(可能布局的标题变量未设置?)

在你解决了这个问题之后,就这样做吧

h1= obj['Team-A'].captain
h2= obj['Team-A'].homeGround
h3= obj['Team-A'].winLoss

这将为您提供数据。

您能展示整个索引jade模板吗?@伙计,就是这样,有一个
扩展布局
语句扩展了我的“母版页”。我对mongo/node/jade非常陌生,所以我不知道如何查询数据库来获取值
Team-a
,以及它的详细信息。这就是我想知道的,您需要了解的使用您的架构,使每个团队都有自己的文档,其中包含一个
名称
字段,用于标识团队名称。是否可以动态获取“team-a”?因为它是我文档的名称。我已更新了我的问题,请看一看您可以检查对象键并将其与正则表达式匹配,/team-[a | B]/,如果它找到了,它会返回它找到的那个,然后你可以用它作为obj[WhatIFoundFromRegex],但这样做有点傻,我会听@johnyhk并更改数据库模式。
var myobj = {
 "Team-A": {
   "captain": "A",
   "homeGround": "some ground",
   "winLoss": "12-4-0"
 },
 "Team-B": {
   "captain": "B",
   "homeGround": "some ground 2",
   "winLoss": "4-4-4"
 }
};

var key = Object.keys(obj)[0] // gives you the first key i.e. Team-A. its an array so every key is easily accessible.
var captain = myobj[key].captain // gives 'A'
var homeGround = myobj[key].homeGround // gives 'some ground'
var winloss = myobj[key].winloss //gives "12-4-0"