使用Node.js和Express.js检索MongoDB子文档

使用Node.js和Express.js检索MongoDB子文档,node.js,mongodb,express,mongo-collection,Node.js,Mongodb,Express,Mongo Collection,我使用node.js、express.js和mongodb创建了一个RESTful API。我通过从MongoDB集合中提取文档开始创建路由,该集合工作得非常好 示例收集文档 { "_id" : ObjectId("51ace8c04cc8ea865df0923e"), "title" : "Some Example Title", "producer" : { "company" : "Your Company Name" } } 有效-如果我使用.find({query

我使用node.js、express.js和mongodb创建了一个RESTful API。我通过从MongoDB集合中提取文档开始创建路由,该集合工作得非常好

示例收集文档

{ 
"_id" : ObjectId("51ace8c04cc8ea865df0923e"), 
"title" : "Some Example Title", 
"producer" : 
 { 
  "company" : "Your Company Name"
 } 
}
有效-如果我使用.find({query})而不是泛型find()

但是,当我试图调用嵌入文档(即子文档)时,它使用点符号,它会中断

*不起作用*

db.something.find( { 'producer.company': 'ABC123' } )
或者即使我尝试

db.something.find( {producer: {company: 'ABC123'} } );
我收到一条错误消息说

TypeError: Converting circular structure to JSON
    at Object.stringify (native)
    at ServerResponse.res.json (../lib/response.js:185:19)
    at ServerResponse.res.send (..//lib/response.js:117:21)
    at ../routes/recipes.js:81:8
    at Collection.find (../node_modules/mongodb/lib/mongodb/collection.js:931:5)
    at ../routes/recipes.js:73:14
    at Db.collection (../lib/mongodb/db.js:462:44)
    at exports.findByCat (../routes/recipes.js:72:5)
    at callbacks (../node_modules/express/lib/router/index.js:161:37)
    at param (../node_modules/express/lib/router/index.js:135:11)
谁能帮我找到一个解决方法,或者让我知道我的方法是否有任何错误


谢谢

你基本上已经做到了

> var a = {a:1}
undefined
> a.b = a
{ a: 1, b: [Circular] }
> JSON.stringify(a)
TypeError: Converting circular structure to JSON
    at Object.stringify (native)
    at repl:1:7
    at REPLServer.self.eval (repl.js:110:21)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.EventEmitter.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)
    at ReadStream.onkeypress (readline.js:99:10)
    at ReadStream.EventEmitter.emit (events.js:98:17)
>变量a={a:1}
未定义
>a.b=a
{a:1,b:[循环]}
>JSON.stringify(a)
TypeError:将循环结构转换为JSON
at Object.stringify(本机)
回复:1:7
在REPLServer.self.eval(repl.js:110:21)
在接口处。(回复js:239:12)
位于Interface.EventEmitter.emit(events.js:95:17)
在接口处在线(readline.js:202:10)
在接口处。\u行(readline.js:531:8)
在接口处写入(readline.js:760:14)
在ReadStream.onkeypress(readline.js:99:10)
在ReadStream.EventEmitter.emit(events.js:98:17)

您正在代码的某个地方创建一个自引用对象

您能描述一下集合中的内容吗?否则,我无法提供从该错误消息中获得的更多信息,这表明json序列化程序正在运行循环参数
> var a = {a:1}
undefined
> a.b = a
{ a: 1, b: [Circular] }
> JSON.stringify(a)
TypeError: Converting circular structure to JSON
    at Object.stringify (native)
    at repl:1:7
    at REPLServer.self.eval (repl.js:110:21)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.EventEmitter.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)
    at ReadStream.onkeypress (readline.js:99:10)
    at ReadStream.EventEmitter.emit (events.js:98:17)