相关疑难解决方法(0)

在mongoose中填充嵌套数组

如何在示例文档中填充"组件":

  {
    "__v": 1,
    "_id": "5252875356f64d6d28000001",
    "pages": [
      {
        "__v": 1,
        "_id": "5252875a56f64d6d28000002",
        "page": {
          "components": [
            "525287a01877a68528000001"
          ]
        }
      }
    ],
    "author": "Book Author",
    "title": "Book Title"
  }
Run Code Online (Sandbox Code Playgroud)

这是我的JS,我通过Mongoose获取文档:

  Project.findById(id).populate('pages').exec(function(err, project) {
    res.json(project);
  });
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js

98
推荐指数
6
解决办法
7万
查看次数

使用Mongoose查询嵌套的嵌入式文档

我正在尝试查询嵌套的嵌入式文档.我试图"填充"结果,但失败了.

如何在查找呼叫中找回所有书籍详细信息?我想要用户架子上的所有书籍对象,我可以从中获取数据吗?

###

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 …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js coffeescript

4
推荐指数
1
解决办法
7148
查看次数

Mongoose深度填充多个第二级对象无法正常工作

我有一个模特A机智这个领域:

var field = {
    foo: String,
    b: [{ type: Schema.Types.ObjectId, ref: 'B' }]
}  
Run Code Online (Sandbox Code Playgroud)

B这个领域的模型:

var field = {
    c: { type: Schema.Types.ObjectId, ref: 'C' } // let's say this has 3 string field
    d: { type: Schema.Types.ObjectId, ref: 'D' } // so was this
}
Run Code Online (Sandbox Code Playgroud)

基于此答案郑氏晃新和成做这个-

 A.find({_id:req.params.id})
    .populate({ path: 'patient', model: Patient,
        populate: {
            path: 'b',
            model: B
        },
        populate: {
            path: 'c',
            model: C
        },
    })
    .exec(function (err, doc) …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js mongoose-populate

3
推荐指数
1
解决办法
1557
查看次数

填充多个子文档

我有一个用户组参考.我想知道我如何填充游戏,用户和组内的排名?所以我基本上想要的是user.group在代码中填充这3个值

用户模式

var userSchema = new Schema({
  fb: {
    type: SchemaTypes.Long,
    required: true,
    unique: true
  },
  name: String,
  birthday: Date,
  country: String,
  image: String,
  group: { type: Schema.Types.ObjectId, ref: 'Group'}

});
Run Code Online (Sandbox Code Playgroud)

小组模型

var groupSchema = new Schema({
  users: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
  }],
  game: { type: Schema.Types.ObjectId, ref: 'Game' },
  ranks: [{
    type: Schema.Types.ObjectId, ref: 'Ladder'
  }]

});
Run Code Online (Sandbox Code Playgroud)

  User.findByIdAndUpdate(params.id, {$set:{group:object._id}}, {new: true}, function(err, user){
    if(err){
      res.send(err);
    } else {
      res.send(user);
    }
  })
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js

2
推荐指数
1
解决办法
989
查看次数