请求投资投资文件 Mongoose

我正在尝试在附加文档内提出一个请求,嵌套。 我试过了 'populate' 获取结果,但它失败了。

如何在搜索电话中返回书的所有详细信息? 我需要收到数据的货架用户上的所有书对象?


###

Trying to query nested embedded documents using Mongoose.

Database Outline for example

An Owner has multiple bookshelves which each have an array of books.
A book is not unique, and the same book could be on many different shelves.

###

mongoose = require/"mongoose"/
Schema = mongoose.Schema
mongoose.connect "localhost", "d1"

bookSchema = new Schema/title: String/
Book = mongoose.model/"Book", bookSchema/

shelfBookSchema = new Schema/
book:
type: Schema.ObjectId
ref: "Book"
/

shelfSchema = new Schema/
name: String
books: [ shelfBookSchema ]
/

Shelf = mongoose.model/"Shelf", shelfSchema/

ownerSchema = new Schema/
firstName: String
shelves: [ shelfSchema ]
/

Owner = mongoose.model/"Owner", ownerSchema/

mongoose.connection.on "open", ->
book1 = new Book/title:"How to make stuff"/
book1.save /err/ ->
throw err if err

owner = new Owner/firstName:"John"/
shelf = new Shelf/name:"DIY Shelf"/
shelf.books.push
_id: book1._id
book: book1._id
owner.shelves.push shelf
owner.save /err/ ->
throw err if err

#Let's find one owner and get all of his bookshelves and the books they containa
Owner.findOne//.populate/"shelves.books.book"/.exec /err, owner/ ->
console.error owner.shelves[0].books

### Log shows:

{ book: 4fe3047401fc23e79c000003,
_id: 4fe3047401fc23e79c000003 }]

Great but how do I get the values of book like the title etc??

###

mongoose.connection.db.dropDatabase ->
mongoose.connection.close//
已邀请:

龙天

赞同来自:

深入的人口被添加到 Mongoose 3.6.
https://github.com/LearnBoost/ ... 11192
对于你的例子,它将是如下:


Owner.find//.populate/'shelves'/.exec/PopulateBooks/;

function PopulateBooks/err, owners/ {
if/err/ throw err;
// Deep population is here
Book.populate/owners, { path: 'shelves.books' }/.exec/callback/;
}

喜特乐

赞同来自:

目前,不支持子集文档子集。 我加入了这个帖子的链接到问题 open github 进一步跟踪。

https://github.com/LearnBoost/mongoose/issues/601

要回复问题请先登录注册